C # declares a 2D array

I am trying to set up a 2d array in C # to act like a maze to move a character around, I have a few questions initializing the array, I am trying to do below

but the InitialiseMaze method says the maze is not declared

Can anyone advise

thank

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
}

      

}

+3


source to share


5 answers


You cannot initialize an array like this other than declaring a variable. However, the change is simple:

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

      



As an aid:

  • It looks like it maze

    should be an instance variable, not a static variable. After all, you initialize it every time you instantiatemaze

  • You have a finalizer for no reason. Finalizers are very rarely required (or really recommended) in C #
+7


source


Just to make John's post more clear:

maze = new int[,]{
{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, 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 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}
};

      



Boy, it was a large array of maze ...

+3


source


Ok, here's an excerpt from msdn:

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

      

retrieved from MSDN multidimensional arrays

you should also read about destructors, finalizers, etc ... I'm sure you come from C ++? The differences between the two languages ​​aren't always obvious :).

+1


source


You need to declare a maze

 numbers = new int[X,Y]; where X and Y are how big it is

      

0


source


2D arrays

The simplest form of a multidimensional array is a two-dimensional array. A two-dimensional array is a list of one-dimensional arrays.

A two-dimensional array can be thought of as a table that has x number of rows and y number of columns. Following is a two dimensional array that contains 3 rows and 4 columns -

enter image description here

2-dimensional arrays in C #

Thus, each element in array a is identified by an element name of the form a [i, j], where a is the name of the array and i and j are indices that uniquely identify each element in array a.

Initializing two-dimensional arrays

int [,] a = new int [3,4] {

   {0, 1, 2, 3} ,   /*  initializers for row indexed by 0 */

   {4, 5, 6, 7} ,   /*  initializers for row indexed by 1 */

   {8, 9, 10, 11}   /*  initializers for row indexed by 2 */

};

      

Explain the code above:

new int [**3**,4] **3** denoting to rows like how may object in array 

eg:

{0, 1, 2, 3} ,  
{4, 5, 6, 7} ,  
{8, 9, 10, 11}


new int [3,**4**] **4** denoting to columns like total value in object (4 columns)

eg:   


   {0, 1, 2, 3}

      

Let's check the program for processing a two-dimensional array

 using System;
    namespace ArrayApplication {
       class MyArray {
          static void Main(string[] args) {
             /* an array with 5 rows and 2 columns*/
             int[,] a = new int[5, 2] {{0,0}, {1,2}, {2,4}, {3,6}, {4,8} };
             int i, j;

             /* output each array element value */
             for (i = 0; i < 5; i++) {

                for (j = 0; j < 2; j++) {
                   Console.WriteLine("a[{0},{1}] = {2}", i, j, a[i,j]);
                }
             }
             Console.ReadKey();
          }
       }
    }

Output:  

a[0,0]: 0
a[0,1]: 0
a[1,0]: 1
a[1,1]: 2
a[2,0]: 2
a[2,1]: 4
a[3,0]: 3
a[3,1]: 6
a[4,0]: 4
a[4,1]: 8

      

0


source







All Articles