Counting variable

Trying to run a series of variables through a counter loop… I can’t figure out how to merge the basic variable and the number… (var1, var2, var3, etc)

Need help right after the comment, please

Dim variable as string
Dim var1 as string
Dim var as string
Var1 = “ABCDE”

Dim y as integer
Y = 1

// help here
Variable = var + y      //?????  How do I join a variable and a number to make one new variable

Do
Textfield.text = variable.totext
Y = y + 1
If y>3 then exit
loop

You can’t. Use an array or dictionary.

[quote=416674:@John Marshall]Trying to run a series of variables through a counter loop… I can’t figure out how to merge the basic variable and the number… (var1, var2, var3, etc)

Need help right after the comment, please

Dim variable as string
Dim var1 as string
Dim var as string
Var1 = “ABCDE”

Dim y as integer
Y = 1

// help here
Variable = var + y      //?????  How do I join a variable and a number to make one new variable

Do
Textfield.text = variable.totext
Y = y + 1
If y>3 then exit
loop

[/code][/quote]

What are you wanting to achieve with:

[code]Variable = var + y      //?????  How do I join a variable and a number to make one new variable

What do you want Variable to contain? If you want to concatenate an integer to a string, you’d need to do:

Variable = var + y.totext

Also, with:

Textfield.text = variable.totext
Since textfield.text expects a string, and Variable is already a string, there’s no need to convert Variable to text using .totext.

Dont use var1, var2, var3,

Use an array

[code]Dim Var(3) as integer

Var(1) = 6
Var(2) = 9
Var(3) = 2

For x as integer = 1 to 3 //this is your loop
Var(x) = x * 2 // note Var(x) achieves the addition of Var and number
next[/code]

[quote=416674:@John Marshall]
// help here
Variable = var + y //??? How do I join a variable and a number to make one new variable

Do
Textfield.text = variable.totext
Y = y + 1
If y>3 then exit
loop

[/code][/quote]

For starters John, a variable can be a number or a string as you may well know.

From what you posted in your original post, you declared var as a string, yet you didn’t assign it a value!

I’ve adjusted the code to a include the assignment of “Var”. btw., it’s good practice to try to stick with capitalisation. Although, as far as I’m aware Xojo doesn’t care if you code “Var1” or “vAR1” - they are treated one and the same, it’s not case sensitive. Nevertheless, it makes it easier for you and others to read if you stick to a style

[code] Dim variable as string
Dim var1 as string
Dim var as string
Var1 = “ABCDE”
var = “XYZ”

Dim y as integer
Y = 1

// shown here:
variable = var + Str(y) // This is one way to join (concatenate) a string and a number to make one new (string) variable.

Textfield1.text = variable[/code]

This works as expected using a push button and a text field. Adding a loop (to increment the number) from this point is easy enough.

If you explain more, then you’ll get better help. This is as far as I can help :slight_smile: