Reputation: 156
My program allows the user to input values of hours and minutes into an array of predefined length. I want to have this button called add for a form I'm creating to allow multiple inputs from the user until the array is fully inhabited. Then when it's done, to call a sortArray() method They way it is now it only allows one input then it throws an exception. How do I go about this?
private void addButton_Click(object sender, EventArgs e)
{
int index = 0;
try
{
while (index <= array.Length)
{
minutes = Int32.Parse(minutesTextBox.Text);
hours = Int32.Parse(hoursTextBox.Text);
MessageBox.Show("You have successfully entered a time!");
array[index] = new RandomClass.Time(hours, minutes);
index = index + 1;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.GetType().FullName);
MessageBox.Show("Please only input integer numbers. Start again from the beginning");
hoursTextBox.Text = null;
minutesTextBox.Text = null;
array = null;
arrayLength = null;
}
MessageBox.Show("Please choose what order you want arrange the sort!");
FileA.RandomClass.sortArray(array);
}
Upvotes: 0
Views: 157
Reputation: 1093
I am basing my answer on the assumption that the add button has to be clicked for every new value of hours and minutes.
Currently I see the following problems in your code
int index = 0;
You are setting the index to zero every time the add button is clicked, due to this array values are overwritten on every click of the add button. Initialize index at a more appropriate place in your code.
while (index <= array.Length)
Let's assume the array is of length 5 and the value of index=5, then this line will cause an error as arrays are zero indexed in c#
minutes = Int32.Parse(minutesTextBox.Text);
Int32.Parse method returns a bool, which represents whether the conversion succeeded or not. A more appropriate way to use int32.Parse would be
Int32.Parse(minutesTextBox.Text, out minutes);
Now, since the new values are added per Add button click you can change the while to if and else. i.e.,
if(index < array.Length)
//Parse, add to array and increment the index
else
//Reset index (if required) and Call the sortArray() method.
Upvotes: 1
Reputation: 1
Dont use this line. You will get IndexOutOfRangeException exception where index will be equal to array length.
while (index <= array.Length)
You should use like this.
while (index < array.Length)
One more this, you can use int.TryParse
to get rid of exception id text is null or emptry.
Upvotes: 1
Reputation: 137148
You haven't shown how large the array is, but this line:
while (index <= array.Length)
will cause a problem when you get to the end of your array because the indices of an array go from zero to one less than the length of the array.
So you need to change this line to either:
while (index < array.Length)
It might safer to loop like this:
foreach (ver element in array)
{
// do stuff
element = new RandomClass.Time(hours, minutes);
}
as this way there is no way you can loop beyond the end of the array.
Upvotes: 1