简体   繁体   中英

C# dynamically access variables based on user input

I am new to C# but have school project that i am working on and need some guidance. I have multiple arrays which i termed array1, array2, array3 , array4 and so on till array9. Now the user is entering digits into input box and i am splitting the input like 543 into 5 4 3 and i want to dynamically call

array5 array4 array3. So basically show contents of these arrays in console.

string value = input.Text.ToString();

 foreach (string s in Array[value[0]])
    {
    -- will loop through all values of array and out put
    foreach (string s in Array[value[1]])
      {
       -- will loop through all values of array and out put
          foreach (string s in Array[value[2]])
          {
          -- will loop through all values of array and out put
          }
       }
    }

How do i accomplish this ? Please help

Try

var arrays = new List<int[]>(){array1,....,array9};

and then if you have 5, 4, 3 you can call arrays like

arrays[4], arrays[3], arrays[2]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM