Using an object array in c#

Aug 31 2008 8:52 PM

I have a user defined object "shape_ref".  The object corosponds to a table row, so that the various columns in the row are properties in the object. 

I am trying to create a function that will return all rows as an object array:

shape_ref[] sr = new shape_ref[30];

I return the rows as a datareader.  I step through the datareader like so (I left out the defining statements for the datareader and command; they work):

Int16 i = 0;

dr = cmd.ExecuteReader();

if (dr.HasRows)

{

while (dr.Read())

{

sr[i].id = Convert.ToInt16(dr["sr_id"]);

sr[i].name = dr["sr_name"].ToString();

sr[i].description = dr["sr_descr"].ToString();

sr[i].image = dr["sr_graphic_path"].ToString();

sr[i].dimCount = Convert.ToInt16(dr["sr_dim_cnt"]);

sr[i].dim1Label = dr["sr_dim_1_label"].ToString();

sr[i].dim2Label = dr["sr_dim_2_label"].ToString();

sr[i].dim3Label = dr["sr_dim_3_label"].ToString();

sr[i].dim4Label = dr["sr_dim_4_label"].ToString();

sr[i].dim5Label = dr["sr_dim_5_label"].ToString();

sr[i].dim6Label = dr["sr_dim_6_label"].ToString();

i = i++;

}

}

The problem I am having is the first time I try to set the value:

sr[i].id = Convert.ToInt16(dr["sr_id"]);

I get an error: "Object reference not set to an instance of an object.", but from what I can see, the "sr[]" IS set using the New statement. 

Anybody see what I'm doing wrong?


Answers (3)