Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# declaring a 2D array

I am trying to setup a 2d array in C# to act as a maze to move a character around, I am having a few issues initialising the array, I am trying to do the below

but the InitialiseMaze method is saying the maze is not declared

Can anyone advise

thanks

simon

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GameMan
{
    public class Maze
    {
       #region Variables
       static int[,] maze;

    #endregion
    #region Constructors/Destructors
    public Maze()
    {
        InitaliseMaze();
    }
    ~Maze()
    {
    }
    #endregion

    #region Methods
    public void InitaliseMaze()
    {

         maze = {
                          {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},    
                          {0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0},     
                          {0, 0, 3, 0, 0, 2, 0, 0, 0, 2, 0, 2, 0, 0, 0, 2, 0, 0, 3, 0, 0},     
                          {0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0},    
                          {0, 0, 2, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 2, 0, 0},    
                          {0, 0, 2, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 2, 2, 0, 0},     
                          {0, 0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0},    
                          {0, 0, 0, 0, 0, 2, 0, 1, 1, 1, 4, 1, 1, 1, 0, 2, 0, 0, 0, 0, 0},     
                          {0, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0},     
                          {1, 1, 1, 1, 1, 2, 1, 1, 0, 0, 0, 0, 0, 1, 1, 2, 1, 1, 1, 1, 1},
                          {0, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0, 1, 1, 2, 0, 0, 0, 0, 0},
                          {0, 0, 0, 0, 0, 2, 0, 1, 1, 1, 1, 1, 1, 1, 0, 2, 0, 0, 0, 0, 0},
                          {0, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0},
                          {0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0},
                          {0, 0, 2, 0, 0, 2, 0, 0, 0, 2, 0, 2, 0, 0, 0, 2, 0 ,0, 2, 0, 0},
                          {0, 0, 3, 2, 0, 2, 2, 2, 2, 2, 5, 2, 2, 2, 2, 2, 0, 2, 3, 0, 0},
                          {0, 0, 0, 2, 0, 2, 0, 2, 0, 0, 0, 0, 0, 2, 0, 2, 0, 2, 0, 0, 0},
                          {0, 0, 2, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 2, 0, 2, 2, 2, 2, 0, 0},
                          {0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0},
                          {0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0},
                          {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
                      };
    }
    #endregion
}

}

like image 441
level_zebra Avatar asked Dec 12 '22 04:12

level_zebra


2 Answers

You can't initialize an array like that other than in a variable declaration. However, the change is simple:

maze = new int[,] { 
   // As before
};

As asides:

  • It looks like maze should be an instance variable rather than a static variable. After all, you're initializing it each time you create an instance of Maze
  • You have a finalizer for no reason. Finalizers are very rarely required (or indeed advisable) in C#
like image 130
Jon Skeet Avatar answered Dec 14 '22 17:12

Jon Skeet


Ok, well here is some extract from the msdn :

 int[,] myArray = {{1,2}, {3,4}, {5,6}, {7,8}};

extracted from MSDN multidimensional arrays

you should also read up concerning Destructors, finalizers etc ... , I bet your coming from C++ ? Differences between the 2 languages arent always obvious :).

like image 38
squelos Avatar answered Dec 14 '22 18:12

squelos