Question Creating multidimensional array of object

wilam

New member
Joined
Dec 8, 2017
Messages
2
Programming Experience
10+
Building a Sudoku solver based around a grid 9x9 of 2 variables.

how do I create this as a class so that I can pass the entire object to its own methods

(so far have tried to create as obj[,] sudokuGrid = new obj[10,]; but can not seem to be able to pass this as param)
 
This declares a 2D array variable:
object[,] sudokuGrid;

In order to create an array of any rank, you must specify the size for every dimension, e.g.
object[,] sudokuGrid = new object[9, 9];

As with any local variable, if you're initialising the variable where you declare it then you can use type inference:
var sudokuGrid = new object[9, 9];
 
Yes and thank you but,

My problem is the next part where I want to encapsulate this as a class so that I can have method which work on the sudoku grid and can be pass as ref object.

If that makes sense

Currently I have to pass something like grid(3,4).anyFullLines but simply want to pass grid as Param.


Ps can't type square brackets so () here lol
 
I'm not 100% sure what you're asking for. An array is a reference-type object. All arrays are instances of the Array class.

Maybe what you need to is to define a class with an array assigned to a field internally and an indexer that can be mapped directly onto the array, e.g.
public class SudokuGrid
{
    private readonly int[,] matrix = new int[9, 9];

    public int this[int x, int y]
    {
        get => matrix[x, y];
        set => matrix[x, y] = value;
    }
}

You can obviously add whatever other members you need too.
 

Latest posts

Back
Top Bottom