Jerett crumbley
Jerett crumbley

Reputation: 3

Foreach going out of bounds while searching through array c#

The purpose of my program is to take a data txt file and edit it, and/or make additions and subtractions to it. The text file format is like this:

Name|Address|Phone|# of people coming|isRSVP

The code I have seems to be doing it's job all the way up until I try to click one of the names within a listbox and it needs to search through the multidimensional array to pull information out and place within a set of textboxes where they can be edited. The problem is that the foreach loop I use gives me an out of bounds exception. I tried to do a step into debug to make sure the data is correct in the array and see the process. Everything seems to do correctly but for some reason in the conditional statement person[0]==listbox1.selectedIndex isn't returning true even though both are the same as I seen through the step into process. Any help would be greatly appreciated. This is my code:

StringBuilder newFile = new StringBuilder();
static  string txtList= "guest_list.txt";    
static string[] file = File.ReadAllLines(txtList);    
static int x = file.Count();
string[,] list = new string[x ,5];

public void loadGuestList()
{

    int count2 = 0;
    foreach (string line in file)
    {
        string[] sliced = line.Split('|');
        int count = 0;

        list[count2, count] = sliced[0];
        count++;
        list[count2, count] = sliced[1];
        count++;
        list[count2,count] = sliced[2];
        count++;
        list[count2,count]= sliced[3];
        count++;
        list[count2, count] = sliced[4];
        count++;
        listBox1.Items.Add(list[count2,0]);
        count2++;
    }
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    foreach (string person in list)
    {
        if (  person[0].ToString()==listBox1.SelectedItem.ToString())
        {
            addTxt.Text = char.ToString(person[1]);
            textPhone.Text = char.ToString(person[2]);
            textPeople.Text = char.ToString(person[3]);
            if (person[4] == 'n' )
            {
            }
            else
            {
                chkRSVP.Checked = true;
            }
            break;
        }
    }
}

Upvotes: 0

Views: 1041

Answers (3)

Orphid
Orphid

Reputation: 2852

In agreement with Adam Gritt, I tested the following code and it seemed to work:

using System;

namespace so_foreach_bounds
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            //System.Text.StringBuilder newFile = new System.Text.StringBuilder();
            string txtList= "guest_list.txt";    
            string[] file = System.IO.File.ReadAllLines(txtList);    
            int x = file.Length;
            System.Collections.Generic.List<string[]> list = new System.Collections.Generic.List<string[]> ();

            foreach (string line in file)
            {
                string[] sliced = line.Split('|');
                list.Add(sliced);
            }

            foreach (string[] person in list)
            {
                Console.WriteLine (String.Join(", ", person));

                if (person[0] =="Gary")
                {
                    string txtAdd = person[1];
                    string txtPhone = person[2];
                    string txtpeople = person[3];
                    if (person[4] == "n" )
                    {
                    }
                    else
                    {
                        bool has_resvped = true;
                    }
                    break;
                }

            }
        }
    }
}

The issue is how you are iterating over the 2d array. It is usually a better idea to create a "Person" class, than try to manage it via arrays though. Oh yes, and it's usually a good idea to check that a list box item is selected before attempting to use one of its members.

Upvotes: 0

Adam Gritt
Adam Gritt

Reputation: 2674

The problem lies in this line:

foreach (string person in list)

The list is defined as being string[,] which when you for each over will do every element, not just the column of data. You really should do something such as:

for(int index = 0; index <= list.GetUpperBound(0); index++)
{
    string slice1 = list[index, 0];
    string slice2 = list[index, 1];
    ....
}

or switch to using a Dictionary<string, string[]>().

Upvotes: 1

Boris
Boris

Reputation: 3

Try to use a "Person" object and override equals(). Right now you're trying to put your multidimensional array (list[0]) into a string, it'll give you a unwanted result. You should use list[0,0] instead.

Upvotes: 0

Related Questions