Android Question [Solved] Difference in ClassModule between getMyValue and Myvalue?

Pflichtfeld

Active Member
Licensed User
Is there a difference between these two sub-declarations in a Class?
B4X:
public Sub getMyvalue As Object
        Return myObject
End Sub
public Sub Myvalue As Object
        Return myObject
End Sub
Both are accessed by
B4X:
xyz=MyCls.Myvalue
or is this wrong?
 

Star-Dust

Expert
Licensed User
Longtime User
They are both correct. but by prefixing get and set you can manage the sub as if it were a variable
B4X:
public Sub getMyvalue As Object
        Return myObject
End Sub
public Sub setMyvalue (Ob As Object)
        myObject=Ob
End Sub

B4X:
xyz=MyCls.Myvalue
MyCls.Myvalue=xyz
 
Last edited:
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Above all, it is useful when you need to publish a value that is not an internal variable of the Class but of an object of the class.
Here is an example:

B4X:
public Sub getMyvalue As Object
        Return myObject.Value
End Sub
public Sub setMyvalue (Ob As Object)
        myObject.Value=Ob
End Sub
It will not be necessary to make the myObject public only to access a property of it
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
Just a small note. If I remember correctly, it's important that the "get" and "set" don't have any CAPS in them.

So this...
B4X:
public Sub SetMyvalue (Ob As Object)
       myObject=Ob
End Sub
...should be...
B4X:
public Sub setMyvalue (Ob As Object)
        myObject=Ob
End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
This is a variable:
B4X:
Public Rotation As Double
to read or change it:
B4X:
MyClass.Rotation = 20.5
MyRotation = MyClass.Rotation
it is only an internal variable accessible from outsides.

This is a property:
get and set must be lower case.
B4X:
'gets or sets the Rotation property of the Chart
'rotates the entire chart
'Rotation in degrees
Public Sub getRotation As Double
    Return Graph.Rotation
End Sub

Public Sub setRotation (Rotation As Double)
    Graph.Rotation = Rotation
    xBase.Rotation = Graph.Rotation
End Sub
to read or change it:
B4X:
MyClass.Rotation = 20.5
MyRotation = MyClass.Rotation
The access code is the same as for the variable, but in the class it's different.
The comments before the Sub declaration are displayed in the inline help in the IDE.

This is a method:
B4X:
Public Sub SetMargins(Left As Int, Right As Int)
    LeftMargin = Left
    RightMargin = Right
End Sub
To use it:
B4X:
MyClass.SetMargins(20, 30)
You can set the values but not read them.
You can, of course, also add comments.
 
Upvote 0

Pflichtfeld

Active Member
Licensed User
Thank you, that clears my question.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
A couple of notes.

1 - Using a Property instead of a variable declared as public in the Process_Globals, has some advantages:
1a - in the two routines that usually make up a Property, you can obviously write code and therefore check that the value set is valid;​
1b - while you cannot prevent the value of a public variable from being changed, you can do it with a Property, creating only the setting routine (setXXX but not getXXX);​

2 - another important difference between a normal public routine and a Property (its "set part") is that only the first one can receive more than one parameter.

3 - I forgot the others I wanted to write, "listening" to the TV news ?
 
Upvote 0

Pflichtfeld

Active Member
Licensed User
2 - another important difference between a normal public routine and a Property (its "set part") is that only the first one can receive more than one parameter.
Ok, thank you for that! (But it applies to the set-routine. My question was about the get.)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…