Counter for variables

BlitzPlus Forums/BlitzPlus Programming/Counter for variables

mattm591(Posted 2007) [#1]
Hi,
I want to have a loop which works like this:
counter=1
total=10
While(counter<total)
label$+counter=CreateLabel("Text"+counter,10*counter,10,40,20,window)
button$+counter=CreateButton("Text"+counter,10*counter,69,20,20,window)
counter=counter+1
Wend

That code works in my head, but not in B+. So can someone explain to me how I would need to change that code so that it would create label1, label2, label3 etc. until it got up to 10.
Thank for any help!
Matt


CS_TBL(Posted 2007) [#2]
It can't.

Apart from the fact that you can't do this, you should also not want this in the first place. That's exactly what arrays are for.

In addition, for-next is more common for these kinda loops.


CS_TBL(Posted 2007) [#3]
dim button(9)
dim label(9)

for t=0 to 9
  button(t)=CreateButton("Text"+(t+1),10*t,10,40,20,window)
  label(t)=CreateButton("Text"+(t+1),10*t,69,20,20,window)
next

Note that it's usually the most 'clean' to use 0-based loops.


mattm591(Posted 2007) [#4]
Ok, thank you. Didn't realise I could use an array.


CS_TBL(Posted 2007) [#5]
One handy thing to remember is that a handle (a window, button, label, bank, timer, textarea, image etc. etc.) is stored as an INT value.

It means that you could store handles in a bank or array. It also means that you can 'view' this handle as an INT value, just try 'debuglog window' if you have a GUI window called 'window', you'll get some high number, which represents the memory address of the window. An INT value is 4 bytes in size.


mattm591(Posted 2007) [#6]
Thanks for the advice :)