Below is the declaration at class level.

int[] PossibleValues = new int[6];
int[][] JaggedArray = new int[50][];
int JaggedVar = 0;

Then in a function, I am assigning PossibleValues array to Jaggedarray like this.

[PossibleNumbers is a function that returns an array of possible numbers for a cell in a grid.]

PossibleValues = PossibleNumbers();
JaggedArray[jaggedVar] = PossibleValues;

The number of elements in PossibleValues in each iteration may vary,but it will never exceed 6.I dont want Jaggedarray to store zeros of PossibleValues so I changed the code to:

PossibleValues = PossibleNumbers();
for (int i = 0; i < PossibleValues.Length; i++)
   if (PossibleValues[i] != 0)
      JaggedArray[jaggedVar][i] = new PossibleValues[i];

But this gives null reference exception at
JaggedArray[jaggedVar][i] = new PossibleValues[i];

I searched it over the internet and came to know that JaggedArray remains null hence values cannot be assigned this way.How to resolve this?
Any help would be appreciated.
Thanks in advance!

I wouldn't recommend using arrays at all in C#. They don't play nice with a lot of other objects.

A list of lists of ints would probably work best for you here:

var JaggedList = new List<List<int>>();
for (int i = 0; i < 50; i++)
    JaggedList.Add(new List<int>(PossibleNumbers()));

You can access the items exactly the same as you could with the jagged array:

int myNumber = JaggedList[5][12];

Edited 4 Years Ago by skatamatic

This article has been dead for over six months. Start a new discussion instead.