locked
Can't figure out exception. RRS feed

  • Question

  • Every time I try to execute one of these menu items I get an exception that says the index is outside the bounds of the array. I can't figure out what I'm doing wrong. My MAX_ARRAY = 5. Any way here's some of the code.
    public struct Employee
            {
                public int employeeID;
                public string employeeName;
                public int totalParts;
                public int defects;
                public double payRate;  
            }
    
            Employee[] Parts = new Employee[MAX_ARRAY];
            short empCount = 0;
            short empSee = 0;
    
            private void defectReportToolStripMenuItem_Click(object sender, EventArgs e)
            {
                // Initialize variables and calculate average and percentage of defects
    
                empCount = 0;
                double oneDefect = 0;
                double allDefects = 0;
                int onePart = 0;
                int allParts = 0;
                double avgDefects = 0;
                double percentDefects = 0;
    
                while (empCount <= MAX_ARRAY)
                {
                    oneDefect = Parts[empCount].defects;
                    onePart = Parts[empCount].totalParts;
    
                    allDefects += oneDefect;
                    allParts += onePart;
    
                    avgDefects = allDefects / empProcessed;
                    percentageDefects = allDefects / allParts;
    
                    empCount++;
                }
    
                averageDefects = avgDefects;
                percentageDefects = percentDefects;
    
                // Display Defect Report
    
                DefectForm aSummaryForm = new DefectForm();
                aSummaryForm.AverageDefects = averageDefects;
                aSummaryForm.PercentageDefects = percentageDefects;
                aSummaryForm.ShowDialog();  
            }
    
            private void lowestPaidEmployeeToolStripMenuItem_Click(object sender, EventArgs e)
            {
                // Initialize variables and calculate lowest paid employee
    
                empCount = 0;
                empSee = 0;
                double currentGrossPay = 0;
                double lowestGrossPay = 0;
    
                while (empCount <= MAX_ARRAY)
                {
                    currentGrossPay = grossPay;
    
                    if (currentGrossPay < lowestGrossPay)
                    {
                        lowestGrossPay = currentGrossPay;
                        empSee = empCount;
                    }
                    empCount++;
                }
    
                Parts[empSee].employeeID = low_EmployeeID;
                Parts[empSee].employeeName = low_EmployeeName;
                lowestGrossPay = low_LowestGrossPay;
    
                // Display Lowest Paid Report
    
                LowestPaidForm aSummaryForm = newLowestPaidForm();
                aSummaryForm.Low_EmployeeID = low_EmployeeID;
                aSummaryForm.Low_EmployeeNamelow_EmployeeName;
                aSummaryForm.Low_LowestGrossPay
    low_LowestGrossPay;
                aSummaryForm.ShowDialog();
            }

    Wednesday, March 24, 2010 8:11 PM

Answers

  • It's because of these lines:

    while (empCount <= MAX_ARRAY)

    Since you are creating the arrays based on MAX_ARRAY you must never index the arrays with a value greater than (MAX_ARRAY - 1).

    So, empCount must always be one less than MAX_ARRAY.
    Change your while conditions into:

    while (empCount < MAX_ARRAY)

    /Calle


    - Still confused, but on a higher level -
    • Proposed as answer by Louis.fr Wednesday, March 24, 2010 11:34 PM
    • Proposed as answer by Louis.fr Wednesday, March 24, 2010 11:34 PM
    • Marked as answer by MarmaladeMan Thursday, March 25, 2010 1:48 AM
    Wednesday, March 24, 2010 8:51 PM