473,378 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Re: Help with declaring and populating an array

On Thu, 14 Aug 2008 18:56:00 -0700, Phill
<Ph***@discussions.microsoft.comwrote:
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
Aug 15 '08 #1
4 1629
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" wrote:
On Thu, 14 Aug 2008 18:56:00 -0700, Phill
<Ph***@discussions.microsoft.comwrote:
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
Aug 15 '08 #2
On Thu, 14 Aug 2008 21:42:03 -0700, Phill
<Ph***@discussions.microsoft.comwrote:
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
Aug 15 '08 #3
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" wrote:
On Thu, 14 Aug 2008 21:42:03 -0700, Phill
<Ph***@discussions.microsoft.comwrote:
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
Aug 16 '08 #4
On Fri, 15 Aug 2008 19:22:11 -0700, Phill
<Ph***@discussions.microsoft.comwrote:
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] };
That suggests that "arrULD" is declared as "double[]" rather than
"double[][]". Unfortunately, you haven't shown all of the code, so
there's no way for us to say exactly what's wrong for sure.

I'm also a little confused, since your first post said you have 7 rows and
8 columns of data, but you seem to be defining here a row of data with 7
columns. But that wouldn't be the compiler error issue, so maybe I
shouldn't concern myself with that.

Pete
Aug 16 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Stefan Richter | last post by:
Hi, I want to create an associative Array with a PHP variable (article ID) as Key and another associative array as it's value. How do I instanciate it, how can I fill it? I want something...
29
by: Friday | last post by:
Sorry if this is the wrong group. I tried to find the one I thought would be most relevant. I'm an old PHP guy, who knows little about asp and NOTHING about asp.net, but need to learn at least...
0
by: r5 | last post by:
I'm using the MIPSpro Compiler and having trouble defining a function template (involving array size specifiers as template arguments) inside a class. The same definition compiles fine outside...
2
by: elmoizwaly | last post by:
hi there i am new to vb i have a new project (mdi) with 8 child forms evry form with at least 10 textboxs. the problem is that when building an array for each form textboxs and declaring the...
8
by: Martin Jørgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
5
by: cmt | last post by:
I have an ASP report that returns values from a SQL database and formats the data in an HTML table. I am trying to figure out a good way of using CSS to highlight the table row that contains the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.