Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从文件读取时,索引超出了数组的边界_C# - Fatal编程技术网

C# 从文件读取时,索引超出了数组的边界

C# 从文件读取时,索引超出了数组的边界,c#,C#,我希望你们都有一个愉快的一天。因此,我修复了程序的一个错误,但还有另一个错误:/ 下面是我创建并从文件中读取数据的代码: void ReadData(string fileName, Branch[] branches) { string shopsName = null; using (StreamReader reader = new StreamReader(@fileName)) { string lin

我希望你们都有一个愉快的一天。因此,我修复了程序的一个错误,但还有另一个错误:/

下面是我创建并从文件中读取数据的代码:

  void ReadData(string fileName, Branch[] branches)
    {
        string shopsName = null;
        using (StreamReader reader = new StreamReader(@fileName))
        {
            string line = null;
            line = reader.ReadLine();
            if (line != null)
            {
                shopsName = line;
            }
            Branch tempBranches = TempBranch(branches, shopsName);
            string address = reader.ReadLine();
            string phoneNumber = reader.ReadLine();
            while (null != (line = reader.ReadLine()))
            {
                string[] values = line.Split(';');
                string facturer = values[0];
                string model = values[1];
                double capacity = double.Parse(values[2]);
                string energyClass = values[3];
                string assemblyType = values[4];
                string color = values[5];
                string attribute = values[6];
                double cost = double.Parse(values[7]);
                Fridges fridge = new Fridges(facturer, model, capacity, energyClass, assemblyType, color, attribute, cost);
                tempBranches.fridges.AddFridge(fridge);
            }
        }
这是我使用TempBranch方法的代码。错误在这一行:
if(分支[i].ShopsName==ShopsName)
。希望你能帮助我,因为我昨天试着解决这个问题30分钟,但它仍然不起作用:D

private static Branch TempBranch(Branch[] branches, string shopsName)
    {
        for (int i = 0; i < MaxNumberOfFridges; i++)
        {
            if (branches[i].ShopsName == shopsName)
            {
                return branches[i];
            }
        }
        return null;
    }
私有静态分支TempBranch(分支[]分支,字符串shopsName)
{
对于(int i=0;i
如果将
maxNumberOfFrids
替换为
分支。Length
它将只尝试查找
分支
数组范围内的分支。它不起作用的原因是,您试图访问的索引大于数组的
长度

如果将
MaxNumberOfFrids
替换为
分支。长度
它只会尝试查找
分支
数组范围内的
分支。它不起作用的原因是您试图访问的索引大于数组的
长度。

试试这个。如果不知道数组的长度,请使用foreach

private static Branch TempBranch(Branch[] branches, string shopsName)
    {
        foreach(var branch in branches)
        {
            if (branch.ShopsName == shopsName)
            {
                return branch;
            }
        }
        return null;
    }

试试这个。如果不知道数组的长度,请使用foreach

private static Branch TempBranch(Branch[] branches, string shopsName)
    {
        foreach(var branch in branches)
        {
            if (branch.ShopsName == shopsName)
            {
                return branch;
            }
        }
        return null;
    }

这引发了错误,因为
maxNumberOfFrids
大于分支
length
。。为了简化它,假设
maxNumberOfFrids
20
,但
arry length
是10,所以您试图访问数组中超出数组长度的元素
11

修理它

  for (int i = 0; i < branches.Length; i++)
    {
        if (branches[i].ShopsName == shopsName)
        {
            return branches[i];
        }
    }

这引发了错误,因为
maxNumberOfFrids
大于分支
length
。。为了简化它,假设
maxNumberOfFrids
20
,但
arry length
是10,所以您试图访问数组中超出数组长度的元素
11

修理它

  for (int i = 0; i < branches.Length; i++)
    {
        if (branches[i].ShopsName == shopsName)
        {
            return branches[i];
        }
    }

您还可以尝试使用LINQ查询

return branches.Where(b => b.ShopsName == shopsName).FirstOrDefault(); 
编辑:

To NullReferenceError发生在您的新帖子中,这是因为在创建店铺的函数中返回null。这是因为找不到给定的店名


因此,它试图在一家不存在的商店里增加一台冰箱,这是不可能的。您必须添加一个检查,以避免出现这种情况。

您还可以尝试使用LINQ查询

return branches.Where(b => b.ShopsName == shopsName).FirstOrDefault(); 
编辑:

To NullReferenceError发生在您的新帖子中,这是因为在创建店铺的函数中返回null。这是因为找不到给定的店名


因此,它试图在一家不存在的商店里增加一台冰箱,这是不可能的。您必须添加一个检查,以避免出现这种情况。

在C中#如果变量实现接口
IEnumerable
,则可以执行
foreach
循环。试试这个:
foreach(branchs中的分支b){if(b.ShopsName==ShopsName)返回b;}
然后在循环之外,你可以返回null,就像你在做正确的事情一样。问题就在这里
int i=0;我有几台冰箱;i++
,因此将其改为
inti=0;i<树枝长度;i++
在C语言中#如果变量实现接口
IEnumerable
,则可以执行
foreach
循环。试试这个:
foreach(branchs中的分支b){if(b.ShopsName==ShopsName)返回b;}
然后在循环之外,你可以返回null,就像你在做正确的事情一样。问题就在这里
int i=0;我有几台冰箱;i++
,因此将其改为
inti=0;i<树枝长度;i++
如果你不知道数组的长度,那么分支的长度呢?是的,你肯定是对的。Tbh foreach是一种更干净的解决方案,可以防止像=等输入错误,语法也更简单。如果你不知道数组的长度,那么分支的长度呢?是的,你肯定是对的。Tbh foreach是一个更干净的解决方案,可以防止像=等输入错误,语法也更简单。