Android Question Variables in a string

msali

Member
Licensed User
Longtime User
Hi,
Is it possible to assign a value to a variable name of which is stored in a string. Following is my scenario.

In main activity i have a few global variables in Type, e.g.,
B4X:
Type MyParameters(NumPlayersPerRound as int, PlaySound as Boolean, _
CourseCondition as string)
Dim tMyP as MyParameters

tMyP.NumPlayersPerRound=6
tMyP.PlaySound=True
tMyP.CourseCondition="Good"

Now I have created another Activity and called it "actSettings" where I have added Views programatically and set their Tag values to the same variables names, e.g.,
Added a Spinner and set its tag value to NumPlayersPerRound
Added a CheckBox and set its tag value to PlaySound
added an EditBox and set its tag value to CourseCondition


Question:
In my actSettings when users change any value, i want to change the values in my Main activity using the variable names in respective Tag values.

I looked into Reflector Lib but have no understanding of it and Java therfore am not sure how to do it.

Pls help as I think it can be done. If i am able to do it this way then my code becomes very small and clean else will have to writh tons of code as i have only given you an example of three variables whereas i have at least 25 of them in my actual code.

Thanks in advance.

regards.
 

Cableguy

Expert
Licensed User
Longtime User
Any object or variable declared in Main activity WILL ALWAYS be accessible by other modules and activities, as the Main activity is ALWAYS started by the app life cycle. So, if you declare all your needed variable under Main Global, they can be accessed from any activity, either to read from or write to. All you need to do is prefix the var name, when you call it from another module, with the origin module name.
In your case ...
B4X:
Main.tMyP
... from your act settings
 
Upvote 0

udg

Expert
Licensed User
Longtime User
AFAIK, you should put your vars in section Process_Globals to have them reachable from any Activity/Module.
Those in Activity Globals are local to that Activity.
BTW, vars declared in Process_Globals often survive the app stop and immediate re-launching due to the fact the process they are part of is not yer terminated by the OS.

udg
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
AFAIK, you should put your vars in section Process_Globals to have them reachable from any Activity/Module.

udg

My mistake. I meant process globals, thanks for the correction.
 
Upvote 0

msali

Member
Licensed User
Longtime User
Yes I am able to access my variables declared in Main as follows:
Main.tMyP.NumPlayersPerRound

No issue in that.

But like i mentioned in my original question I have programatically created around 15 CheckBoxes based on variables defined in my Type data type (MyParameters).
Eeach checkbox represent a variable in MyParameters type.
While adding them to my view programatically i placed the variable name in CheckBox.Tag. Now when the users checks it or un-checks it i want to set the related paramenter variable accordingly.

Currently the only solution that I have is using a Select Case...End Select and set it manually in each case of View's tag value.

So my question is that is there any way in reflector Lib or any other Lib where I can use the variable name as set in view's tag.

Hope I am able to explain my scenario better, this time around.

Here is a sample code of what I am trying to do

B4X:
' Code in Main Activity

Type MyParameters(AutoMoveToNextHole as Boolean, PlaySound as Boolean, UsePhoneContacts as Boolean, _
    SendScoreCardViaEmail as Boolean, SendScoreCardViaSMS as Boolean, SendScoreCardViaWhatsapp as Boolean)

Dim tMyP as MyParameters

tMyP.AutoMoveToNextHole=False
tMyP.PlaySound=True
tMyP.UsePhoneContacts=True
tMyP.SendScoreCardViaEmail=True
tMyP.SendScoreCardViaSMS=False
tMyP.SendScoreCardViaWhatsapp=True

In any other activity I am able to use and set these variables

B4X:
'in any other activity

If Main.tMyP.AutoMoveToNextHole=False Then
  GoToNextHole
End if
Main.tMyP.PlaySound=False
.
.
.
and so on...

Now in my actSettings I have following code:
B4X:
Sub CreateChkBox(VariableName as String)
  dim chk as CheckBox
  chk.Initialize("chkbox")
  chk.Tag= VariableName     ' e.g., VariableName=AutoMoveToNextHole, PlaySound ... etc
  .
  .
  .
End Sub

Now the issue is in checkbox_CheckedChange

B4X:
Sub chkbox_CheckedChange(Checked As Boolean)
    ' this is where i need HELP
    Dim chk as CheckBox
    chk=Sender
    ' what can i do do set the var value
    Main.tMyP.chk.Tag=False ' <---- Need hlep in this line
End Sub
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Unfortunately, AFAIK there's no simple solution to what you'd like to achieve.
Read this post from Erel.

Umberto
 
Upvote 0

msali

Member
Licensed User
Longtime User
I wish there was. It would have had helped a lot.

Just out of Curiosity - isnt there anything like this in reflector eiter? (I am not familiar with Reflector at all)

Erel: can this be added to wishlist. If yes then I will surly be renewing my license then again - whenever it comes out.

Thanks any way for all your responses. I have implemented it with Case statements.

regards.
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
The only way to achieve what you want at present is to use a map as explained here...
http://www.b4x.com/android/forum/th...-with-a-variable-or-string.37101/#post-218622
This was specifically for B4J but it will work just the same when used in B4A.
Slightly off topic, but B4PPC (the windows mobile platform which is no longer developed) used to be able to use strings to refer to objects. I think the keyword was Control(VariableString).Property although I could be wrong on the syntax. It would be nice to have the same option in B4A but I believe it is now better practice to pass the object rather than a variable.

Regards,
RandomCoder
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
As previously said, you can use a map. However, since you're talking about settings, I would go for a sqlite table, setting id, description, value, and then updating it based on its description's (or id's) value (since this is what you want to do). One advantage would be that user's preferences are stored for a later session. Another option, is to use Preferences. Anyway, in reality, they're all some sort of maps :)
 
Upvote 0
Top