Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help in array of array of classes needed

Status
Not open for further replies.

razovy

Programmer
Feb 10, 2004
4
US
I want to create class Matrix which contains array of class Rows.
Class Rows contains array of Cells.

Here is the declaration:

public class Cells
{
public int value = 0;
public int color = 0;
public int background = 0;

}
public class Rows
{
public Cells [] cell = new Cells [10];
public int row_num;
}

public class Matrix
{
public Rows []row = new Rows [100];
public int new_id;
// Constructor
public Matrix (){
. . .
for (int i=0; i<10; i++){
for (int j=0; j<10; j++){
row.cell[j] = i+j;
}
}
. . .
}

}



Creation of class Matrix element

Matrix mtr = new Matrix();


causes Error in Constructor in “row.cell[j] = i+j;”
“Object reference not set to an instance of an object”.
Rows are nulls and Cells are missing entirely when I tried to watch an Object!!!

Obviously something missing in Constructor, but I have no idea how to fix it.

Please help
 
Try moving the code that populates your matrix out of the constructor and into it's own method:
Code:
public class Matrix
{
   public Rows []row = new Rows [100];
   public int new_id;

   // Constructor
   public Matrix ()
   {
      // initialize member variables here
      new_id = int.MinValue;
   }

   public void Initialize()
   {
      for (int i=0; i<10; i++)
         for (int j=0; j<10; j++)
            row[i].cell[j] = i+j;
   }
}
Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top