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

Fin Warman
  • 43
  • 1
  • 8
  • 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. – InBetween Jan 31 '16 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. – Fin Warman Jan 31 '16 at 20:14

3 Answers3

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 },
    };
romanoza
  • 4,775
  • 3
  • 27
  • 44
  • 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. – Fin Warman Jan 31 '16 at 20:10
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.

Community
  • 1
  • 1
  • 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. – Fin Warman Jan 31 '16 at 20:06
  • The problem was not in the array syntax, but the variable declaration. I hope other will find your link helpful too. Thanks. – Fin Warman Jan 31 '16 at 20:15
  • 1
    Yeah, sorry - I misinterpreted your code and thought A1 A2 B1 and B2 were strings. – Peter Tsrunchev Jan 31 '16 at 20:17
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 },
        };
Ricky Keane
  • 1,540
  • 2
  • 15
  • 21
  • Thank you, but this was simply a code-copying error and not the actual problem I have. I have edited it now. – Fin Warman Jan 31 '16 at 20:04