1

I have checked a previous question similar however the provided solution did not work for me, or perhaps I am overlooking something. How to return a jagged array

My code is as follows:

class Grids
{
    int[][] grid;
    public grid[][] MakeGrid() 
    {
        grid = new int [4][];
        for (int i = 0; i < 4; i++)
        {
            grid[i] = new int[4] {0,0,0,0};
            for (int a = 0; a < 4; a++)
            {
                Console.Write(grid[i][a]);
            }
            Console.WriteLine();
        }
        return;
    }
}

The error returned is as follows:

.Grids.grid' is a 'field' but is used like a 'type'

1
  • 1
    Your should return int[][] from your MakeGrid method, shouldn't you? Commented Jul 18, 2016 at 9:14

1 Answer 1

5

Some minor adjustments required in your MakeGrid method:

public int[][] MakeGrid()
{
    grid  = new int[4][];
    for (int i = 0; i < 4; i++)
    {
        grid[i] = new int[4] { 0, 0, 0, 0 };
        for (int a = 0; a < 4; a++)
        {
            Console.Write(grid[i][a]);
        }
        Console.WriteLine();
    }
    return grid;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Sadiq hit the nail. You can't use your property as return type of your MakeGrid method.
In addition, put int[][] in front of grid and get rid of the global variable.

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.