none
Variable Variables in C# RRS feed

  • Question

  • Hey!, I coded in Autohotkey back in the day and to create variable you would do this:

    ExampleVar = 1
    
    Targetvar%ExampleVar% = 1

    By doing that you would create new var Targetvar1 = 1

    int i = 1; while (i <= 10) { variable%i% = i; Print(variable%i%); i++; }

    //Expected Output: //Variable: "variable1" Output: "1" //Variable: "variable2" Output: "2" //Variable: "variable3" Output: "3"

    That's what i got up to in C# but the % are underlined red.


    • Edited by Andynatoor Thursday, February 28, 2019 2:09 PM
    Thursday, February 28, 2019 2:08 PM

All replies

  • Try the following which are not variables but could be placed into a list or dictionary

    var index = 1;
    while (index <= 10)
    {
        Console.WriteLine($"variable = {index}");
        index += 1;
    }

    Then read string interpolation.

    Dictionary example.

    var values = Enumerable.Range(1, 10).Select(item => item).ToArray();
    var dictionary = values.ToDictionary(v => v, v => true);

    Why the above, because you can't create variables "on the fly" like you want


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites



    Thursday, February 28, 2019 2:25 PM
    Moderator
  • Hello,

    > in C# 
    Who told you that the code you provide are in C#?

    You have to study C#. 


    Sincerely, Highly skilled coding monkey.

    Thursday, February 28, 2019 2:28 PM
  • >Try

    He want to create a variable (variable name) at execution time... 


    Sincerely, Highly skilled coding monkey.

    Thursday, February 28, 2019 2:30 PM
  • i see what you tried to do there. but i wanted to create 10 new variables or update them.

    variable1 = 1;

    variable2 = 2;

    etc.

    Thursday, February 28, 2019 2:33 PM
  • its a mix of autohotkey and c# just as example as i don't know what is the name of thing i am trying to achieve.

    trying to automatically create variables with while.

    Thursday, February 28, 2019 2:35 PM
  • You have to describe 10 separate variables and initialize them

    or describe an array of variables and than initialize them.

    C# works much differently than Autohotkey. You have to study C# to be able to use it. 


    Sincerely, Highly skilled coding monkey.


    Thursday, February 28, 2019 2:37 PM
  • its a mix of autohotkey and c# just as example as i don't know what is the name of thing i am trying to achieve.

    trying to automatically create variables with while.

    You don't create single variables, instead use a Dictionary or similar container to store multiple values. Also the scope is dependent on your needs e.g. it could be private for one class or form. 

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, February 28, 2019 2:48 PM
    Moderator
  • >Try

    He want to create a variable (variable name) at execution time... 


    Sincerely, Highly skilled coding monkey.

    Yes I know but that is not the right way to go about it.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, February 28, 2019 2:48 PM
    Moderator
  • if I already declared these variables before, would I be able to update their values then?

    and yes im learning now through trial and error :)


    so how would it look like if i used this?

    public int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; foreach (int number in numbers) { variable[number] = number;

    Console.WriteLine(variable[number]);

    }

    this of course doesn't work but im close


    • Edited by Andynatoor Thursday, February 28, 2019 2:52 PM
    Thursday, February 28, 2019 2:51 PM
  • >trying to automatically create variables with while.

    Again - C# works differently than Autohotkey.

    In C# you can't just create a variable and use it where you like to have it - there are a rules of when it's created and how it created and also when it not longer available, This rules are completely different from Authohotkey.

    For 10 variables will be reasonable to have an array. 
    But! I addition to have an array you may need to initialize each variable in array. Or you may not need to initialize them. Depends on what type of variable you need.

    Study C# - it it would be much easier than use a "spear method".


    Sincerely, Highly skilled coding monkey.

    Thursday, February 28, 2019 2:51 PM
  • would I be able to update their values then?

    int position = 7;
    int newvalue = 1234;
    numbers[position] = newvalue;
    Approach is different...


    Sincerely, Highly skilled coding monkey.

    Thursday, February 28, 2019 2:54 PM
  • I just wanted to shorten my code considerably but it seems to work differently in C# that's why I couldn't find what i was looking on google for quite some time.. "variable variables" haha

    Thanks for being helpful everyone! :)

    Thursday, February 28, 2019 3:03 PM
  • I just wanted to shorten my code considerably but it seems to work differently in C# that's why I couldn't find what i was looking on google for quite some time.. "variable variables" haha

    Thanks for being helpful everyone! :)

    As mentioned before this is not possible like it is in JavaScript.

    $(document).ready(function () {
        var varname = "karen";
        for (var index = 1; index < 11; index++) {
            var temp = varname + index;
            eval('var ' + temp + '=' + index);
            console.log(temp);
        }
    });


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, February 28, 2019 4:05 PM
    Moderator