0

Simple Problem, but I can't find an answer. I already asked this question but it has been marked as a duplicate to "What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?" Which is a solution I have already read and not found an answer in. Please help, much thanks.

public static int tileValue;
    public static int layers;
    public static int rows;
    public static int columns;

    static public int Layer;
    static public int Row;
    static public int Col;

    static public int[,,] map = new int[Layer, Row, Col];// <<<<<<<<<<<<<<<<<<< FIX THIS, i dont make it??

    public static int Level(int layer, int row, int column)
    {
        int tileIndex = 0;
        if (Layer == 2 && Row == 10)
        {
            //map.SetValue(4, layers, rows, columns);
            tileIndex = (int)map.GetValue(0, 0, 0);
        }

        return tileIndex;
    }

I have no idea why I am getting a System.IndexOutOfRangeException. I tested with the variable of Layer, Row and Col to 2, 10, 10, which is why I had the if condition and the code only works if I put in the numbers manually. As,

 static public int[,,] map = new int[2, 10, 10];

the Level method tileindex (for now) checks the value of 0,0,0; the smallest value to check and returns the value to a different class. Yet, I still receive the error. I know the problem is with my

static public int[,,] map = new int

Because it only works when I manually input it. And I used the if condition to see if the numbers were inputing well, and they are... So solutions?

Sean Yi
  • 29
  • 4
  • 2
    You haven't assigned a value to 'Layer', 'Row' and 'Col'? Those fields will always be zero. Is your code snippet incomplete? Do you want to assign the parameter values to your static public fields? In that case, the following is missing in your 'Level' method: Layer = layer; , Row = row, Col = column; – Odrai Dec 03 '17 at 16:11
  • I did!! Hehe There set within a different class – Sean Yi Dec 03 '17 at 16:13
  • Are you sure you set them *before* the class is used? My guess is no. You should definitely know the information from [this answer](https://stackoverflow.com/a/3681278/1141432). Use the debugger and see what the values are for those when you encounter the error. – itsme86 Dec 03 '17 at 16:16
  • The issue is the **static** `map` **property**. Calling the class which contains the `Level` method, creates an instance of `map` with Layer = 0, Row = 0 and Col = 0. Setting the Layer, Row and Col afterwards doesn't recreate the `map` field. Please read [Static Classes and Static Class Members (C# Programming Guide)](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members). – Odrai Dec 03 '17 at 16:18
  • You guys are life savers, the problem was with the static Layer, Row and Col. I am currently looking the problem, but how would you guys recommend I fix it? I was initializing the variables in the Game1() and using the Level method in the Draw(). Much thanks guys – Sean Yi Dec 03 '17 at 16:30

0 Answers0