Android Question Is there a way to access a variable using a compound string

Gary Milne

Active Member
Licensed User
Longtime User
This is a simple example:

Dim XYZ as My_Class
Dim XYZ_InterestingValue as Int
XYZ_InterestingValue = 999

I want the Class to be able to look up the value Main.XYZ_InterestingValue

When the Class is initialized I know the name is XYZ but I don't know how to compound the two known pieces together.

Inside the class I want to be able to do something like
Dim I as Int
I = EventName & "_InterestingValue" 'where the EventName is XYZ

Now I would be 999

Any suggestions appreciated.
 

Gary Milne

Active Member
Licensed User
Longtime User
Thanks for the response, although the same basic question was asked the answer does not apply in my case as the value of "XYZ" could be anything and my class does not know it until RunTime.
 
Upvote 0

Gary Milne

Active Member
Licensed User
Longtime User
Try to define the variable InterestingValue as Public in the Class.
Then you can access it in Main with XYZ.InterestingValue.
That would work if I knew the value of XYZ when I compile the program but XYZ represents the name the developer uses when they Dim XYZ as MyClass. However a developer my call my class ABC, DEF, F1, etc.
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
See if this helps you out any:
B4X:
Sub GetVariableValueJO(o As Object, varName As String) As Object
    Dim jo As JavaObject = o
    Return jo.GetField("_" & varName.ToLowerCase)
End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You could define InterestingValue as a Property of the class.
B4X:
Sub Class_Globals
   Private cInterestingValue As Int
End Sub

Public Sub getInterestingValue As Int
    Return cInterestingValue
End Sub

Public Sub setInterestingValue(InterestingValue As Int)
    cInterestingValue = InterestingValue
End Sub

Then in the Main module:
XYZ.InterestingValue = 12

You'll see it also in the inline help.

upload_2015-10-31_12-4-42.png
 
Upvote 0

Gary Milne

Active Member
Licensed User
Longtime User
I appreciate you taking the time but I must not have been very clear.

In the example of a Callback from a class to the main activity I can tell the CallBack to go to EventName & "_Click" where Eventname is a variable defined by the developer at Initialization.

Now take the same idea where I want the class to reach back to Main and grab the contents of a variable called EventName & "_Variable"

In Main.
Dim MyName as Class
Dim X as MyName_InterestingValue = 25

From inside MyName I want to be able to reach back to Main and grab the variable MyName_InterestingValue by using a compound name MyName (which is variable and controlled by the developer) and appending "_InterestingValue"

Basically I'm trying to get a Class to do a polling operation on a variable inside Main when I can't hardcode the variable name because it's dynamic.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Basically I'm trying to get a Class to do a polling operation on a variable inside Main when I can't hardcode the variable name because it's dynamic.
Sorry but I don't really understand the problem nor what exactly you want to do.
What exactly is dynamic and what for ?
Do you have a concrete example of your needs and problem ?
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
I think, Gary wants to Evaluate an expression, in his case it would be EVALUATE("XYZ_InterestingValue"), and EVALUATE should returns value of the var XYZ_InterestingValue, ie, 999.
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
I think so, but Gary should give a better explanation of what he wants, cause he's talking about variables and classes, so, I don't know
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
If I understand Gary correctly, he wants a function that will take as an argument the name of a variable in String form and return the value of that variable. My response in post #6 does that. An example usage:
B4X:
Sub Process_Globals
   Dim aBc as Int = 12
End Sub

Sub Activity_Resume
   Log("value of aBc: " & GetVariableValueJO(Me, "aBc"))
End Sub
 
Upvote 0

Gary Milne

Active Member
Licensed User
Longtime User
If I understand Gary correctly, he wants a function that will take as an argument the name of a variable in String form and return the value of that variable. My response in post #6 does that. An example usage:
B4X:
Sub Process_Globals
   Dim aBc as Int = 12
End Sub

Sub Activity_Resume
   Log("value of aBc: " & GetVariableValueJO(Me, "aBc"))
End Sub

Yes, this is it exactly. I looked at your prior post and didn't quite get how to use it but the example makes it very clear.

My purpose for this is as follows in case someone is wondering what I could possibly use this for.

I have a class, lets call it MyDoohickey.

I dim as follows:
Dim DH1 as MyDoohickey
Dim DH2 as MyDoohickey
Dim DH1_Value
Dim DH2_Value

Inside the MyDoohickey class I have a Timer that polls the parent and grabs whatever is in the variable that corresponds to it's name. In order to do that I needed to compound the name the class was created under (DH1, DH2) and then append "_Value" so that DH1 grabs the value from DH1_Value and DH2 grabs the value from DH2_Value.

I know that I could do this the other way, by setting up a timer in the parent and pushing the value to the class. However, I think that this is a much cleaner solution as the timers can sit dormant inside the class until they are needed. If you invoked 6 of these classes the timer code inside the parent would start to get unwieldy.

Thank you everyone for your responses and I hope this is of use to others.
 
Upvote 0
Top