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: Copy
public Sub getMyvalue As Object
        Return myObject
End Sub
public Sub Myvalue As Object
        Return myObject
End Sub
Both are accessed by
B4X: Copy
xyz=MyCls.Myvalue
or is this wrong?
 
Is there a difference between these two sub-declarations in a Class?
B4X: Copy
public Sub getMyvalue As Object
        Return myObject
End Sub
public Sub Myvalue As Object
        Return myObject
End Sub
Both are accessed by
B4X: Copy
xyz=MyCls.Myvalue
or is this wrong?
They are both correct. but by prefixing get and set you can manage the sub as if it were a variable
B4X: Copy
public Sub getMyvalue As Object
        Return myObject
End Sub
public Sub setMyvalue (Ob As Object)
        myObject=Ob
End Sub

B4X: Copy
xyz=MyCls.Myvalue
MyCls.Myvalue=xyz
 
Last edited:
Upvote 0
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: Copy
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
This is a variable:
B4X: Copy
Public Rotation As Double
to read or change it:
B4X: Copy
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: Copy
'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: Copy
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: Copy
Public Sub SetMargins(Left As Int, Right As Int)
    LeftMargin = Left
    RightMargin = Right
End Sub
To use it:
B4X: Copy
MyClass.SetMargins(20, 30)
You can set the values but not read them.
You can, of course, also add comments.
 
Upvote 0
to read or change it:
B4X: Copy
MyRotation = MyClass.Rotation
it is only an internal variable accessible from outsides.
to read or change it:
B4X: Copy
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.
Thank you, that clears my question.
 
Upvote 0
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
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
Back
Top