1

When I press the click event to add array values, it gives me an out of index exception error. I'd like to know why it's happening and how to fix it, thanks.

using System;
using System.Windows.Forms;


namespace ArrayTestCalculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }



        double total = 0;
        double average = 0;
        double scoreInput = 0;
        int countOfExams = 0;
        double[] examScores;
        double count = 0;
        //field array declare


        private void enterBtn_Click(object sender, EventArgs e)
        {
            countOfExams = int.Parse(numOfExamsTextBox.Text);
            examScores = new double[countOfExams];
            examLabel.Text = "Exam 1: ";


            label1.Visible = false;
            numOfExamsTextBox.Visible = false;
            enterBtn.Visible = false;

            examLabel.Visible = true;
            examScoresTextBox.Visible = true;
            addBtn.Visible = true;


        }



        private void addBtn_Click(object sender, EventArgs e)
        {
            examLabel.Text = "Exam " + (countOfExams +1) + ":";
            examScores[countOfExams] = double.Parse(examScoresTextBox.Text);
            countOfExams++;
            examScoresTextBox.ResetText();
            examScoresTextBox.Focus();


            if(countOfExams >= examScores.Length)
            {
                MessageBox.Show("hoise vaya");
            }


        }

        private void clearBtn_Click(object sender, EventArgs e)
        {
            Array.Clear(examScores, 0, examScores.Length);
            ClearAll();
        }
        private void exitBtn_Click(object sender, EventArgs e)
        {
            this.Close();                       
        }

        public void ClearAll()
        {
            examLabel.Text = "Exam:";
            examScoresTextBox.ResetText();
            numOfExamsTextBox.ResetText();

        }
        private void calcBtn_Click(object sender, EventArgs e)
        {

        }


    }
}
David Pilkington
  • 13,528
  • 3
  • 41
  • 73

2 Answers2

1

double[] examScores; has been defined as array, which was never initialized if you are not calling enterBtn_Click first Ideally,

You should first initialize/ check if it is initialized it by

double[] examScores = new double[<length>];

also, the counter starts from 0 and not 1

If you are not sure about the length or capacity, use List

A3006
  • 1,051
  • 1
  • 11
  • 28
  • Thanks but I've initialized the array inside the enterBtn click event. The length of the array depends on numOfExamsTextBox which will be a user input. – Abir Choudhury Feb 16 '17 at 04:49
  • If the number of exams if flexible, why not use List instead of array? – A3006 Feb 16 '17 at 04:51
1

In the first line of your enterBtn_Click,

countOfExams = int.Parse(numOfExamsTextBox.Text);

I suppose you are using countOfExams as the total length of the array.

Later in the second line of your addBtn_Click,

examScores[countOfExams] = double.Parse(examScoresTextBox.Text);
countOfExams++;

I suppose you are using countOfExams to track the actual number of exams. Because countOfExams is already set to the length, it results in OutOfRangeException.

Thus, I suggest you to use another variable (e.g. a local variable) for the total length of the array,

var size = int.Parse(numOfExamsTextBox.Text);
examScores = new double[size];
Dante May Code
  • 11,177
  • 9
  • 49
  • 81