Counting variable

  1. last week
    Edited last week

    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
    
  2. Tim H

    Dec 3 Pre-Release Testers Portland, OR USA

    @John M How do I join a variable and a number to make one new variable

    You can't. Use an array or dictionary.

  3. Tim S

    Dec 4 Canterbury, UK

    @John M 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

    What are you wanting to achieve with:

    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.

  4. Jeff T

    Dec 4 Midlands of England, Europe
    Edited last week

    Dont use var1, var2, var3,

    Use an array

    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
  5. Steve K

    Dec 4 Melbourne, Australia
    Edited last week

    @John M // 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]

    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

     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

    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 :)

or Sign Up to reply!