Help with declaring and populating an array

  • Thread starter Thread starter Phill
  • Start date Start date
P

Phill

I have a table that contains 7 row of data with 8 columns. The columns
contain decimal data. Looks like this:
1 .5 .5 0 0 0 0 0 0
2 .25 .25 .25 0 0 0 0 0
etc.
The first column tells me how many points and I will use this as my index in
the array. I want to load this table into an array to be used in some
calculations and i don't want to have to read the database everytime they
change the number of points to calculate. I am new to c# and am having a lot
of trouble declaring my array and populating it. This is what I have so far
but it won't compile because the array is defined wrong..I think??? Please
help.

_conn = new
SqlCeConnection(PocketRigger.LocalStorage.ConnectionString);
string sSQL = "SELECT ULD_Number1, ULD_Number2, ULD_Number3,
ULD_Number4, ULD_Number5, ULD_Number6, ULD_Number7, ULD_Number8,
NumberOfPoints FROM tblUniformDistributionFormula ORDER BY NumberOfPoints";
double[,] arrULD=new int[8];

SqlCeCommand cmd = new SqlCeCommand(sSQL, _conn);
cmd.Connection.Open();
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
arrULD[(int)rdr["NumberOfPoints"]] =
{(double)rdr[0],(double)rdr[1]};
}
 
Thanks for the response. This line is still giving an error:
while (rdr.Read())
{
arrULD[(int)rdr["NumberOfPoints"]] = {rdr[0],rdr[1]};

//{rdr[0],rdr[1]};
}
The error is ";" expected in this part {rdr[0],rdr[1]};

Peter Duniho said:
I have a table that contains 7 row of data with 8 columns. The columns
contain decimal data. Looks like this:
1 .5 .5 0 0 0 0 0 0
2 .25 .25 .25 0 0 0 0 0
etc.
The first column tells me how many points and I will use this as my
index in
the array. I want to load this table into an array to be used in some
calculations and i don't want to have to read the database everytime they
change the number of points to calculate. I am new to c# and am having
a lot
of trouble declaring my array and populating it. This is what I have so
far
but it won't compile because the array is defined wrong..I think???

For future reference, if you are asking for help with an error (compile or
execution), you really should post the complete text of the error and be
specific about when and where it happens.

That said, in your code it's clear what's wrong:
double[,] arrULD=new int[8];

If you declare a multi-dimensional array, you have to allocate one. And
you can't allocate just one dimension. A proper allocation would look
like "new int[rows, 8]" (if "rows" contains the number of rows you want)
or "new int[7, 8]" (if you know ahead of time you want 7 rows), or
whatever.

Now, that said, based on the code you posted, it seems as though maybe you
don't really want a multidimensional array anyway. In your loop, you are
setting an element of a single-dimension array to an instance of another
single-dimensional array. For that, your array initialization would look
more like this:

double[][] arrULD = new double[][7]; // 7 rows of data

Then the code in your loop would work fine.

Finally, don't forget that C# arrays are 0-based. :)

Pete
 
Thanks, I have looked at many sample, none are helping me though. Now I get
error saying cannot convert type double[] to double on this line:
arrULD[(int)rdr["NumberOfPoints"]] = new double[] {
(double)rdr[0], (double)rdr[1], (double)rdr[2], (double)rdr[3],
(double)rdr[4], (double)rdr[5], (double)rdr[6] };

This should not be rocket science....urrrr

Peter Duniho said:
Thanks for the response. This line is still giving an error:
while (rdr.Read())
{
arrULD[(int)rdr["NumberOfPoints"]] = {rdr[0],rdr[1]};

//{rdr[0],rdr[1]};
}
The error is ";" expected in this part {rdr[0],rdr[1]};

Sorry, yes...the short-hand collection notation is allowed only in
initializers. Change the right-hand-side to "new double[] { rdr[0],
rdr[1] }" and it should work.

For future reference, you should become familiar with the C# language
reference on MSDN. It has lots of information about basic syntax for
expressions, assignments, declarations, etc.

Pete
 
Back
Top