-1

I creating one program, but on function of creating entry I have problem! Code:

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":

class variables
    {
        public static string[] addrStr = { };
        public static string[] valueStr = { };
        public static string[] addrInt = { };
        public static int[] valueInt = { };
        public static int addrStrC, addrIntC;
    }
oleg002
  • 3
  • 4
  • 2
    IndexOutOfRangeException occurs when you are trying to access a variable from an array or list that is not there. For example: `int[] number = { 0, 1, 2 }; int number2 = number[7];` There is no 7th variable in `number`. That might not help, but this is the most likely reason why you are being thrown the error. [error](https://learn.microsoft.com/en-us/dotnet/api/system.indexoutofrangeexception?view=net-5.0) – gbe May 17 '21 at 17:41

1 Answers1

2

I'm sure the problem is on this line:

variables.addrStr[variables.addrStrC] = name;

Because the array is initially empty, as in this line: public static string[] addrStr = { }; and then you try to assign something to the first element of an empty array (that has 0 element), and so it throws the exception.