0

I need a two-dimensional array of objects as a field in a class "World". When the class "World" is initialized, I do not yet know the size of the two-dimensional array, since this is set in a GUI after initialization. How do I do this? I have searched for examples where the array is initialized as empty, but how do I then resize the array after initialization? I am probably approaching this situation in the wrong way, but since I need the field in the world object and don't know the size of the array at initialization, I'm stuck. Any advice would be appreciated.

I tried initializing an empty array, but can't find how to resize a multi-dimensional array. I also thought about putting the array in another class, but in such a case that class would need to be initialized as well, so that won't solve this problem. A List is probably not the solution since I'm working with a two-dimensional grid, and I assume a two-dimensional array would be most appropriate for that. Below is the relevant example. Without initializing the squareGrid array in the constructor, I get "Severity Code Description Project File Line Suppression State Warning CS8618 Non-nullable field 'squareGrid' must contain a non-null value when exiting constructor. Consider declaring the field as nullable."

    public class World
{
    Square[,] squareGrid;

    public World(ref DimensionsAndStuff dimstuffIn)
    {
        // some other stuff going on
    }
}
  • 1
    Array.Resize(ref YourArray, SizeYouWant); – CSharp-n Feb 20 '23 at 14:41
  • 3
    Define your array as `myLovelyClass[,] array` and then create the appropriate size when knowing the dimension `array = new myLovelyClass[myXDimension, myYDimension]`. A single Dimension would also easily work it is just a bit of different simple math moving between indexes. – Ralf Feb 20 '23 at 14:47
  • I would at least consider creating your own 2D array type that wraps a 1D array. Some things, like serializing data, tend to be easier for 1D arrays, since multi dimensional arrays tend to not be as well supported by tools and libraries. – JonasH Feb 20 '23 at 15:12
  • Your question reads as if you do not actually need to resize the Array, just initialize it to the correct size at a later point in time? In that case, @Ralf s suggestion will work just fine. No need to actually initialize the array during startup – Jannik Michel Feb 20 '23 at 15:17

1 Answers1

0

You can just instantiate the array after you know its dimensions by using a method that takes the dimensions as parameters e.g.

public class World
{
    private int[,] _coordinates;
    
    public void SetupCoordinatesGrid(int xSize, int ySize)
    {
        _coordinates = new int[xSize, ySize];
    }
}

Then you would setup the multi-dimensional array after you know the dimensions.

var w = new World();
w.SetupCoordinatesGrid(xValue, yValue);// where xValue & yValue are existing variables set via user input
Update

If you have "nullable reference types" enabled then explicitly mark _coordinates as nullable i.e.

public class World
{
    private int[,]? _coordinates;
    
    public void SetupCoordinatesGrid(int xSize, int ySize)
    {
        _coordinates = new int[xSize, ySize];
    }
}
YungDeiza
  • 3,128
  • 1
  • 7
  • 32
  • That does not seem to work. If I don't initialize the array in the constructor of the class "World", where the array is declared, I get "Severity Code Description Project File Line Suppression State Warning CS8618 Non-nullable field 'squareGrid' must contain a non-null value when exiting constructor. Consider declaring the field as nullable. " – Developer Nightlife Feb 20 '23 at 16:00
  • I see. You must have nullable reference types enabled. You can either disable it (https://stackoverflow.com/a/74370168/19214431) or explicitly mark `_coordinates` as nullable. – YungDeiza Feb 20 '23 at 16:19
  • 1
    Yes! This seems to solve it! Thank you! – Developer Nightlife Feb 20 '23 at 16:33