0

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);

    }
6
  • So, what the question? Commented Oct 23, 2016 at 16:34
  • 1
    what exception you get? Commented Oct 23, 2016 at 16:39
  • I agree with tym32167... I am not clear on what you are asking. One problem I can see is that would throw an exception is in the Int32.Parse(..) if the user enters letters you will get an exception when you try to use it. i would recommend checking for valid input. Where do you get the error? Commented Oct 23, 2016 at 16:46
  • i want to allow the user to keep adding values using the add button and after the array is fully inhabited to call a method. How do i go about doing this? Commented Oct 23, 2016 at 16:49
  • 1
    Sounds like a loop that will stop when the array is full. And you have part of that, however, both answers indicate an out of bounds problem there, Get that fixed, then inside that loop start a while loop to get VALID integers from the user keep looping until you get valid integers or exit. Once you have the valid integers put them in the array, not sure what the RandomClass is doing while inserting the numbers into the array. But if the numbers get placed, start the process over on the next hours minutes or exit if the array is full. Hope that helps. Commented Oct 23, 2016 at 17:04

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

1

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.

1 Comment

Glad I could help :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.