Bug? Get and Set not working?

keirS

Well-Known Member
Licensed User
Longtime User
If define a class like.

B4X:
Sub Class_Globals
    Private Property1 As String
    Private Property2 As Int
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize

End Sub

Sub setProperty1(Property_1 As String)
    Property1 = Property_1
End Sub

Sub getProperty1() As String
    Return Property1
End Sub

Sub setProperty2(Property_2 As String)
    Property2 = Property_2
End Sub

Sub getProperty2() As String
    Return Property2
End Sub

As I expected the auto complete hides the get and set methods. However it does not show Property1 or Property2.

NuISqQIL.png
 

klaus

Expert
Licensed User
Longtime User
You should declare the routines as Public.
B4X:
Public Sub setProperty1(Property_1 AsString)
    Property1 = Property_1
End Sub
This is not madatory but adviced.

Property_2 is declared as a Int.
But in the routines you defined a String !?

Then you have the property variable names the same as the property name this won't work.
Change Property1 to mProperty1 and Property2 to mProperty2 and it will work.
 

keirS

Well-Known Member
Licensed User
Longtime User
By default Subs are public so adding a Public declaration doesn't change a thing. This still produces the same result.

B4X:
Sub Class_Globals
    Private Property1 As String
    Private Property2 As String
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize

End Sub

 Public Sub setProperty1(Property_1 As String)
    Property1 = Property_1
End Sub

Public Sub getProperty1() As String
    Return Property1
End Sub

Public Sub setProperty2(Property_2 As String)
    Property2 = Property_2
End Sub

Public Sub getProperty2() As String
    Return Property2
End Sub

My parameter variable names aren't the same as the property names. The variable names have an underscore in them:

Properties:
Property1
Property2

Parameters:
Property_1
Property_2
 

stevel05

Expert
Licensed User
Longtime User
Yes, but the getter and setter methods lose the get/set from the front of them when they are accessed. This is then the same as your Variable names: setProperty1 becomes Property1.

As Klaus says, if you change the Global definitions to mProperty1 etc, it will work.
 

keirS

Well-Known Member
Licensed User
Longtime User
Yes, but the getter and setter methods lose the get/set from the front of them when they are accessed. This is then the same as your Variable names: setProperty1 becomes Property1.

As Klaus says, if you change the Global definitions to mProperty1 etc, it will work.

I misunderstood what Klaus meant. To me a field of a class that uses getters and setters is a property of the class and not a variable. This is the bug I am trying to point out. You get a warning if you define a sub with the same name as a property. There is no warning when doing this and B4J will happily compile the class with no errors.
 

jmon

Well-Known Member
Licensed User
Longtime User
The problem is because you set the Class_Globals variable as private with the same name as your methods.

Private Property1 AsString
==
setProperty1

(if you consider that B4J will remove the "set" prefix)
Check if you don't have a warning in the logs saying that you have a Global variable named the same way as your method
 
Top