3

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'.

2
  • You are creating a 2 dimensional array of arrays. That is, every element of your array is an array itself. An array is a reference type and as such, it's default value is null. Think about what happens when you make an array of strings; the elements of the array will be null until you assign them a value, well your case is essentially the same thing. You need to initialize every element of Map[,] because they will not automatically reference empty arrays as it seems you are expecting. Commented Jan 31, 2016 at 20:07
  • Thank you, I made a rookie mistake or assigning variables after referencing them and still expecting them to have the value assigned. This was the cause of the problem. Commented Jan 31, 2016 at 20:14

3 Answers 3

2

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 },
    };
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you!I feel silly for not noticing the wrong initialisation order, I was so used to freely-ordered class definitions that I forgot it doesn't work with variable assignment. You have saved me a lot of confusion.
1

A two dimensional array is essentially an array of equally sized arrays. You can visualize it as a matrix, with each column (or row, doesn't really matter) as a separate array. So then the elements in the first row (column) would be the first elements of each of the arrays. What you are doing is trying to create a two dimensional array of arrays.

The best answer on this is a great read concerning your question.

3 Comments

I have read and re-read this several times. It proved very helpful in my understand of multidimensional arrays. I cannot figure out where the issue of not being able to reference items the array arises.
The problem was not in the array syntax, but the variable declaration. I hope other will find your link helpful too. Thanks.
Yeah, sorry - I misinterpreted your code and thought A1 A2 B1 and B2 were strings.
0

Your signature for Map is incorrect, it can't be void and a multi-dimensional string array. Change it to:

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

1 Comment

Thank you, but this was simply a code-copying error and not the actual problem I have. I have edited it now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.