Foreach 在搜索数组 C# 时越界

Foreach going out of bounds while searching through array c#

我的程序的目的是获取一个数据txt文件并对其进行编辑,and/or对其进行增减。 文本文件格式是这样的:

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

我的代码似乎一直在执行它的工作,直到我尝试单击 列表框 中的其中一个名称并且它需要搜索多维数组提取信息并将其放入一组 文本框 中,以便对其进行编辑。问题是我使用的 foreach 循环给了我一个 out of bounds 异常。我尝试进行调试以确保数组中的数据正确并查看过程。一切似乎都正确,但由于某种原因,条件语句 person[0]==listbox1.selectedIndex 没有返回 true,即使两者都与我在进入过程中看到的相同。任何帮助将不胜感激。 这是我的代码:

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

尝试使用 "Person" 对象并覆盖 equals()。现在你正试图将你的多维数组 (list[0]) 放入一个字符串中,它会给你一个不需要的结果。您应该改用 list[0,0]。

问题出在这一行:

foreach (string person in list)

该列表被定义为 string[,],当您每次结束时将执行每个元素,而不仅仅是数据列。你真的应该做这样的事情:

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

或改用 Dictionary<string, string[]>()

与 Adam Gritt 达成一致,我测试了以下代码,它似乎有效:

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

            }
        }
    }
}

问题在于您如何遍历二维数组。创建 "Person" class 通常比尝试通过数组管理它更好。哦,是的,在尝试使用其中一个成员之前检查列表框项目是否被选中通常是个好主意。