loop to create variable names and reference them

sturdyboyrich

New Member
HI all,

I'm driving myself nuts trying to figure out how to reference a variable based on a incremented loop value..

so say i have a load of variables

loopnum = 2
offset1 =2
offset2 =5

then i do a loop

For la=1 To lasnum-1
offx = "offset" & la
Next

how do i reference the variable offset1 based on the la value...

sorry I'm sure this is really basic but I just can't figure it out..

Thanks

Rich
 

agraham

Expert
Licensed User
Longtime User
There is no indirect addressing for variables. By the way the name of a Label variable bears no relation ship to the actual Label instance.
B4X:
Dim L1, L2, L3 as Label
L1.Initialize("L1") ' this instance will call the L1_Click event Sub
L2.Initialize("L2") ' this instance will call the L21_Click event Sub
L3 = L1
L1 = L2 ' L1 now references the instance declared in L2
L2 = L3 ' L2 now references the instance declared in L1
You could collect the Label instances in a Map and address them with a calculated string value

B4X:
Dim LMap As Map
For I = 0 to 10
   Dim L as Label
   L.Initialise("Label" & I)
   Lmap.Put(("Label" & I, L)
Next
...

Dim L as Label
For I = 0 to 10
   L = Map.Get("Label" & I)
   L.DoSomething
   ...
Next
 
Upvote 0

RichardBernard

Member
Licensed User
Longtime User
Thank you very much Agraham! Now what if the labels were set in the Designer (which means they've already been initialized)? How does the process change?

R
 
Upvote 0
Top