Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use a variable number in the name of a variable name?

Tags:

variables

c#

wpf

(using WPF) Is it possible to use the number of a TextBox to select the parameter name? For example, the user can put 0,1,2 or 3 as text in the TextBox:

private void button1_Click(object sender, RoutedEventArgs e)
{
    int test = int.Parse(textBox1.Text);

    string nr1 = "this string is 1";
    string nr2 = "this string is 2";
    string nr3 = "this string is 3";

    MessageBox.Show();
}

Now for MessageBox.Show(); in want to put something as nr(test)
Would something like this be possible?

like image 396
Dante1986 Avatar asked Dec 07 '25 23:12

Dante1986


1 Answers

Why not use a Dictionary ?

var dict = new Dictionary<int,string>();
dict.Add(1,"String Number 1");
dict.Add(2,"String Number 2");
dict.Add(3,"String Number 3");

int test = int.Parse(textBox1.Text);
MessageBox.Show(dict[test]);
like image 124
Richard Friend Avatar answered Dec 10 '25 12:12

Richard Friend



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!