Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use variables on variable name?

Tags:

c#

for (int f = 1; f <= 6; f++)
{
    textBox{f+11} = (loto[f].ToString());
}

Hi again, I'm trying to learn c# on my own. Sorry for this noobish questions :)

To be more spesific, that's what I want :

A shortcut to write codes like that :

textBox12.Text = loto[1].ToString();
textBox11.Text = loto[2].ToString();
textBox10.Text = loto[3].ToString();
textBox9.Text = loto[4].ToString();
textBox8.Text = loto[5].ToString();
textBox7.Text = loto[6].ToString();

This code is working but i want to write it in a for loop

like image 566
1342 Avatar asked Mar 17 '26 01:03

1342


1 Answers

You can use a dictionary.

Dictionary<int, TextBox> dictionary = new Dictionary<int, TextBox>();

dictionary.Add(1, textbox1);
... // add the other textboxes


// access the dictionary via index
dictionary[f+11] = ...
like image 109
citronas Avatar answered Mar 18 '26 16:03

citronas