Wish Class get and set properties to include index option

SteveTerrell

Active Member
Licensed User
Longtime User
getValue and setValue on properties in a class are a good feature. Could this be expanded to include an index to help with accessing arrays/lists/maps in the class in the same way?

i.e.
getArrayValue(index As Int) As Float
setArrayValue(index As Int, newValue As Float)

The use being:

instance.ArrayValue(3) = 22.7
or
value = instance.ArrayValue(3)
 
Last edited:

qsrtech

Active Member
Licensed User
Longtime User
+1 (but I know it's gonna be challenging otherwise it would have already been done?)
 

LucaMs

Expert
Licensed User
Longtime User
I know this is not exactly what you need, but...

B4X:
' Main
Sub Activity_Resume
  Person.SetPhoneNum(2, "555-123-456")
  Log(Person.GetPhoneNum(2))
End Sub


'clsPerson - Class module
Sub Class_Globals
  Private mPhoneNums(4) As String
End Sub

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

Public Sub SetPhoneNum(Index As Int, PhoneNum As String)
  mPhoneNums(Index) = PhoneNum
End Sub

Public Sub GetPhoneNum(Index As Int) As String
  Return mPhoneNums(Index)
End Sub
 

qsrtech

Active Member
Licensed User
Longtime User
That's the correct way to implement it in the class, unfortunately you would still need to use the "get/set" when you use the class In an activity.
 

qsrtech

Active Member
Licensed User
Longtime User
Build a quick demo and try using the class in an activity. You'll be able to use it but you have to use it like sub calls vs properties. Like I said the code is right it just doesn't work as a true property.
 

SteveTerrell

Active Member
Licensed User
Longtime User
Thanks LucaMs,

As you show it is easy to implement in code.

What I was wishing for, as qsrtech has been saying, was the class syntax version where the get and set are replaced by a reference to the underlying sub call (property) name (as they are for the existing property get/set syntax) but with the additional index parameter.

It is only a "nice to have" and a natural extension to the recent property addition to classes (which are really syntactic sugar as Erel points out in the tutorial)
 

LucaMs

Expert
Licensed User
Longtime User
You're welcome (I have not been able to give a great help).

If I remember correctly (damn memory) a long time ago Properties was only variables.

They have been deliberately "replaced by" routines (almost certainly to be able to perform validations on them).

I just can not see a major difference between the two syntaxes:

B4X:
instance.ArrayValue(3) = 22.7
instance.SetPhoneNum(2, "555-123-456") ' could be: instance.PhoneNum(2, "555-123-456")

value = instance.ArrayValue(3)
value = instance.GetPhoneNum(2)

Regards
 

SteveTerrell

Active Member
Licensed User
Longtime User
There is no execution difference. Its all down to whether it is "nicer" to access variables (or get/set functions) in a class as properties.

The major difference is in the "set" which would be written as an assignment when the property syntax is used.

instance.PhoneNum(3) = "555-123-456"

In the calling routine it would look as if you are accessing the PhoneNum array directly but of course you are really calling the setPhoneNum sub which can check the index, value being assigned etc.

This can make the calling routine easier to read/understand.

There would be nothing preventing the sub form if that is "better"

instance.PhoneNumSet(3, "555-123-456")
 
Top