【问题标题】:System.IndexOutOfRangeException in standart int variable [duplicate]标准 int 变量中的 System.IndexOutOfRangeException [重复]
【发布时间】:2021-05-17 17:31:07
【问题描述】:

我正在创建一个程序,但是在创建条目的功能上我有问题! 代码:

string cmd = Console.ReadLine();
if (cmd.Contains("varStr"))
            {
                string name = cmd.Remove(0, 7); // No problems!
                variables.addrStr[variables.addrStrC] = name; // No problems!
                variables.addrStrC++; // There is problem!!!
                Console.WriteLine("OK!"); // This is needs for indicating state
            }

类“变量”:

class variables
    {
        public static string[] addrStr = { };
        public static string[] valueStr = { };
        public static string[] addrInt = { };
        public static int[] valueInt = { };
        public static int addrStrC, addrIntC;
    }

【问题讨论】:

  • IndexOutOfRangeException 发生在您尝试访问不存在的数组或列表中的变量时。例如:int[] number = { 0, 1, 2 }; int number2 = number[7];number 中没有第 7 个变量。这可能无济于事,但这是您被抛出错误的最可能原因。 error

标签: c# variables integer


【解决方案1】:

我确定问题出在这一行:

variables.addrStr[variables.addrStrC] = name;

因为数组最初是空的,如以下行:public static string[] addrStr = { };,然后您尝试将某些内容分配给空数组的第一个元素(具有 0 元素),因此它会引发异常。

【讨论】:

  • 谢谢!这是帮帮我!
猜你喜欢
  • 2019-05-13
  • 1970-01-01
  • 2014-06-03
  • 2014-08-25
  • 2011-11-28
  • 1970-01-01
  • 2013-10-04
  • 2011-05-29
  • 2017-08-13
相关资源
最近更新 更多