I am new to C#. I have created a random class that manages to generate some not so very random numbers. But my problem is that I want to output these numbers to 6 different text boxes. I'm sure sure there is a more efficient way of doing it which is also less long winded. and this is how I have done it:
protected void genBtn_Click(object sender, EventArgs e)
{
Random RandomClass = new Random();
int num1 , num2, num3 , num4, num5 , num6;
num1 = RandomClass.Next(1,49);
num2 = RandomClass.Next(1,49);
num3 = RandomClass.Next(1,49);
num4 = RandomClass.Next(1,49);
num5 = RandomClass.Next(1,49);
num6 = RandomClass.Next(1,49);
TextBox1.Text = num1.ToString();
TextBox2.Text = num2.ToString();
TextBox3.Text = num3.ToString();
TextBox4.Text = num4.ToString();
TextBox5.Text = num5.ToString();
TextBox6.Text = num6.ToString();
Create an array, then loop?
Random RandomClass = new Random();
Control[] textboxes = new Contro[] {TextBox1,TextBox2,TextBox3,TextBox4,TextBox5,TextBox6};
foreach(Control c in textboxes)
c.Text = RandomClass.Next(1,49).ToString();
Or a List<TextBox>
then ForEach
?
List<TextBox> textboxes = new List<TextBox>() {TextBox1,TextBox2,TextBox3,TextBox4,TextBox5,TextBox6};
textboxes.ForEach(x => x.Text = RandomClass.Next(1,49).ToString());