Android Question [ANSWERED] Is there a way to get the value of a Class Globals variable programmatically?

Lee Gillie CCP

Active Member
Licensed User
Longtime User
I have an instance of the class in an Object.
I have the name of the Class Globals variable in a string.

CallSub doesn't seem to do it.

What I need is the VALUE of the Class Globals variable for a class instance, where I have the name of the variable held in a string.

Why am I trying to do this? This is part of a larger goal to create JSON serialization for class object instances generically and recursively, which is apparently JSON 1.10 does not do. The platform I am inter-operating with deals with serializing classes automatically/generically just fine. For those classes I want to be able to serialize I implement an individualized GetSerializableFields Sub which returns a map of field names and their types which are to be serialized.
 
Last edited:

Roycefer

Well-Known Member
Licensed User
Longtime User
You should create an accessor method in that class that uses the name of your variable. For example:
B4X:
'Class module for YourClass
Sub Class_Globals
     Private ThisInt as Int = 5
End Sub

Public Sub GetThisInt as Int
     Return ThisInt
End Sub
Then, you can create your Strings dynamically and feed them to CallSub. Example:
B4X:
Dim VarNameFromJSON as String = "ThisInt"
Dim IntValueFromJSON as Int = CallSub(YourClass, "Get" & VarNameFromJSON)
 
Upvote 0

Lee Gillie CCP

Active Member
Licensed User
Longtime User
Thank you for responding!

Your reply makes sense. FWIW I had made my class globals PUBLIC. I was hoping to not have to add more baggage to each class to support serialization. Some of these classes have 30-50 fields. ... But sometimes you just gotta do what you gotta do. Having a reflection library that would operate on B4A class objects would be truly awesome. I have used it to get the type name of a class object, but no luck calling member functions. The ability to query the interface meta data would also be huge.
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
You can use Reflection to do all that but it is generally messy and dangerous. You'll have to look at the compiled .java files to get the true class names, variable names and method names, though. Typically, everything is made lower case by the B4X transpiler and usually variable names and method names have an underscore prepended to the lower case name. Sometimes, you'll also have to use fully qualified names so you'll have to get package names to make that work. With Reflection, you can access private data members and private methods. But, like I said, it's dangerous. You should know exactly what these classes are doing, internally.

To extend the previous example, you could access ThisInt by using Reflection to access "_thisint". But since it's private, you might have to change a "isaccessible" attribute to true. It's tricky and brittle stuff. I think it's a more robust solution to add accessors and go that route.

As a third option, if I need JSON serialization in my classes, I build it into the class. I include a .toJSON() method and an .InitializeFromJSON() method in the class. This would save you from having to use Reflection or use unnecessary accessors.
 
Upvote 0

Lee Gillie CCP

Active Member
Licensed User
Longtime User
Roycefer.... You advanced my understanding hugely. Thank you for sharing your precious experience, wisdom and knowledge. A much better plan is forming.
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
My pleasure.

Feel free to share the overall architecture of whatever solution you end up using in this thread as many others will undoubtedly face this problem and seek a solution in the forums.
 
Upvote 0
Top