S
S
se2711962021-03-17 10:11:55
Arrays
se271196, 2021-03-17 10:11:55

How to create an array of lists through C# classes?

Good afternoon. As planned, I have an array (map), each cell can contain many values ​​\u200b\u200bwith which I will need to work. Decided to implement through classes.

class map
    {       
        public  Coordinates[,] Coor;
        //параметры карты
        public int n; 
        public int m;
        public   map(int n) { this.Coor = new Coordinates[n, n];
            this.n = n; this.m = n;
        }
        public map(int n, int m) { this.Coor = new Coordinates[n, m]; this.n = n; this.m = m; }
    }
class Coordinates
    {
        public   List<int> Type=new List<int>();
    }

map Map = new map(2);
Map.Coor[1,1].Type.Add(1);
Map.Coor[1,1].Type.Add(2);
Map.Coor[0, 0].Type.Add(2);
Console.WriteLine("count "+Map.Coor[1, 1].Type.Count);

I get the error "Object reference not set to an instance of an object." I tried many combinations, but I don't know how to fix it. who knows the solution?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
freeExec, 2021-03-17
@se271196

You have created an empty array Coorfor the elements. But you don't have the elements themselves. It is necessary new Coordinatesto do on all array, each element. And learn how to debug your code.

A
Alexander Lykasov, 2021-03-17
@lykasov-aleksandr

How to look for the causes of such similar errors:

  • Set a breakpoint on the line with the error
  • We look at what is there with the values ​​​​of variables

In your case:
6051b0621d2aa442080624.jpeg
We see that all elements of Coor are null.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question