Help in array of array of classes needed

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to create class Matrix which contains array of class Rows and some additions
Class Rows contains array of Cells

Here is the declaration

public class Cell

public int value = 0
public int color = 0
public int background = 0


public class Row

public Cells [] cell = new Cells [10]
public int row_num


public class Matri

public Rows []row = new Rows [100]
public int new_id
// Constructo
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 elemen

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!!

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

Please hel
 
The expression

new Cells[10]

creates an array of 10 Cell objects; each item in the array is initialized
to its default value (which is null in the case of reference types). You
must still create the individual elements in the array.

public class Rows
{
public Cells[] cells;
const int CellsCount = 10;

public Rows()
{
this.cells = new Cells[CellsCount];

for(int i=0; i<CellsCount; i++)
{
this.cells = new Cells();
}
}
}
 
I'd made a changes you suggested - Same Error on
row.cell[j].value = i+j
statemen


public class Cell

public int value = 0
public int color = 0
public int background = 0


public class Row

public Cells[] cell
const int CellsCount = 10
public int row_num
public Rows(

this.cell = new Cells[CellsCount]

for(int i=0; i<CellsCount; i++

this.cell = new Cells()





public class Matri

public Rows []row = new Rows [100]
public int new_id
// Constructo
public Matrix (

for (int i=0; i<10; i++

for (int j=0; j<10; j++

row.cell[j].value = i+j






---------

Matrix mtr is created here

private void button1_Click(object sender, System.EventArgs e

Matrix mtr = new Matrix()
 
It's not working, Ted:

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

}
public class Rows
{
public Cells[] cell;
const int CellsCount = 10;
public int row_num;
public Rows()
{
this.cell = new Cells[CellsCount];

for(int i=0; i<CellsCount; i++)
{
this.cell = new Cells();
}
}
}


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].value = i+j;
}
}
}
}


Call is from here:

private void button1_Click(object sender, System.EventArgs e)
{
Matrix mtr = new Matrix();
}


Would you like to copy-paste this code and try to run it?
I'll be very appreciated.
 
Desparate (more) said:
I'd made a changes you suggested - Same Error on
row.cell[j].value = i+j;
statement


Yes, because you've got the same problem with the row variable - you've
created the array, but not populated it with rows.

(Note that your code creates an array with 100 rows in it, but your
loop only looks at 10 of them, by the way.)
 
Back
Top