Jump to content
Sign in to follow this  
mcclane654

create multiple variables with a while loop

Recommended Posts

mcclane654

Local $counter = 0
While $counter < 5
    $var$counter = 1+$counter
    $counter=$counter+1
WEnd

GUICreate("test")
GUISetState()
GUICtrlCreateLabel($var0, 5, 20)
GUICtrlCreateLabel($var1, 5, 40)
GUICtrlCreateLabel($var2, 5, 60)
GUICtrlCreateLabel($var3, 5, 80)
GUICtrlCreateLabel($var4, 5, 100)

i would like my $var to have the counter number added to its name. is this even possible if it is how?

Share this post


Link to post
Share on other sites
water

Using arrays makes it much easier than adding the index to the variable name.

Means: Use $var[0] instead of $var0.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
Rogue5099

Does this help at all?

Local $var[1], $counter = 0
While $counter < 5
    $var[$counter] = 1 + $counter
    $counter = $counter + 1
    ReDim $var[UBound($var) + 1]
WEnd

GUICreate("test")
GUISetState(@SW_SHOW)
For $i = 0 To $counter
    GUICtrlCreateLabel($var[$i], 5, 20 * $i)
Next

Share this post


Link to post
Share on other sites
monax

 

Does this help at all?

Local $var[1], $counter = 0
While $counter < 5
    $var[$counter] = 1 + $counter
    $counter = $counter + 1
    ReDim $var[UBound($var) + 1]
WEnd

GUICreate("test")
GUISetState(@SW_SHOW)
For $i = 0 To $counter
    GUICtrlCreateLabel($var[$i], 5, 20 * $i)
Next

 

Maybe Im wrong, but i think changing an array size inside a loop is a not a good thing.

Instead it should create at start an array with the final size. If the amount of elements that will be assigned to the array is unknow before run the program, then you should use a guess value for the array size and after the loop use the ReDim to reduce the array size.

In this case the final size is know, so the code could be:

$max = 5
Local $var[$max], $counter = 0
While $counter < $max
    $var[$counter] = 1 + $counter
    $counter += 1
WEnd

GUICreate("test")
GUISetState(@SW_SHOW)
For $i = 0 To $counter-1
   GUICtrlCreateLabel($var[$i], 5, 20 * $i)
Next
 
sleep(3000) ;Time to see the GUI before the program end

Share this post


Link to post
Share on other sites
Rogue5099

If the amount of elements that will be assigned to the array is unknow before run the program, then you should use a guess value for the array size and after the loop use the ReDim to reduce the array size.

This "guessing" the size of an unknown array could cause for a "Array variable has incorrect number of subscripts or subscript dimension range exceeded" error and that could lead to it not working at all.  There is nothing wrong with re-sizing an array within a loop.  If there is a known amount then you could set the size at the beginning and not use another variable as follows:

Local $var[6], $counter = 0
While $counter < UBound($var) - 1
    $var[$counter] = 1 + $counter
    $counter += 1
WEnd

GUICreate("test")
GUISetState(@SW_SHOW)
For $i = 0 To $counter
    GUICtrlCreateLabel($var[$i], 5, 20 * $i)
Next

Share this post


Link to post
Share on other sites
akorx

Is there a way to create directly the name of the variables in loop ? like something like that "dim $p$i" where $i increase each time ?

Edited by akorx

AkorxMail akorx@yahoo.fr

Share this post


Link to post
Share on other sites
water

That's what arrays are being made for.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×