Help in array of array of classes needed

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
 
T

Ted Miller

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();
}
}
}
 
G

Guest

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()
 
R

razovy

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.
 
J

Jon Skeet [C# MVP]

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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top