Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - 2D Array of String Arrays

I am trying to create a world map for a console-based game.

In the World class I have string arrays that can be printed using a level print method.

public static string[] A1 = new string[]
{
    (" level data"),
    (" level data"),
};

This works if I do

Render.DrawLevel(World.A1);

where Render.DrawLevel takes args:

(string[] _level)

However, in the World Class, I want to create a two-dimensional array that can hold all of the Map's string arrays.

I have tried:

public static string[,][] Map = new string[,][]
{
    { A1 , A2 },
    { B1 , B2 },
};

But if I try to use:

World.Map[0,0]

as my arguments for level printed, I get given an error that this is NULL, instead of the string[] A1.

How can I create a 2d array of arrays, and refer to it properly for string[] arguments?

Thank you very much,

Fin.

EDIT: Incorrectly copied code as 'static void' not just 'static'.

like image 987
Fin Warman Avatar asked Nov 24 '25 08:11

Fin Warman


1 Answers

Place the Map initialization after the initialization of the arrays. A1, A2, B1, B2 are equal to null during the initialization of Map.

The syntax is perfectly fine.

This works as intended:

    public static string[] A1 = new string[]
        {
            (" level data 1"),
            (" level data 2"),
        };

    public static string[] A2 = new string[]
        {
            (" level data 1"),
            (" level data 2"),
        };

    public static string[] B1 = new string[]
        {
            (" level data 1"),
            (" level data 2"),
        };

    public static string[] B2 = new string[]
        {
            (" level data 1"),
            (" level data 2"),
        };

    public static string[,][] Map = new string[,][]
    {
        { A1, A2 },
        { B1, B2 },
    };
like image 87
romanoza Avatar answered Nov 27 '25 00:11

romanoza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!