0

Hi,
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;
jaggedVar++;

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];
   }
}
jaggedVar++;

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!

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by thines01
0

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 by skatamatic

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.