3
Answers

Array /Subscript

Farhad Ahmad

Farhad Ahmad

7y
421
1

Please see codes below, I did realize my mistake, (1) Modularizing the codes as below is not needed (2) once i call the method RandomNumber( Int Number2)  in the loop, each time the loop runs it create a new object and save value returned, so all five values are in separate objects. 

 However what i am not getting is when codes executed all five array elements  have the same value.
 
Please see below 
{
public partial class Form1 : Form
{
private const int Number2 = 200;
public Form1()
{
InitializeComponent();
}
private int RandomNumber(int Number2)
{
int Number;
Random rand = new Random();
Number = rand.Next(Number2);
return Number;
}
private void GenerateN_Click(object sender, EventArgs e)
{
int Number = 0;
int size = 5;
int[] NumberArray = new int[size];
for (int index = 0; index < size; index++)
{
Number = RandomNumber(Number2);
NumberArray[index] = Number;
}
DigitLabel1.Text = NumberArray[0].ToString();
DigitLabel2.Text = NumberArray[1].ToString();
DigitLabel3.Text = NumberArray[2].ToString();
DigitLabel4.Text = NumberArray[3].ToString();
DigitLabel5.Text = NumberArray[4].ToString();
}
}
}
 
 
 
Answers (3)