from List <double> to double[]

W

Web learner

The following code works fine

private List<double> GetDataFor(string column, int selectedYear) {
-------
-------
return list;
}

foreach (double item in GetDataFor("AirTemp", selectedYear)) {
Response.Write(item.ToString() + ", ");
}

It shows data as follows:

37987, -2.42, 37988, -2.41, 37989, -3.34,....................................

where the five digit numbers correspond to date (the first one for 01 Jan 2004) and the decimal negative number as AirTemp. As you can see both are of type double.

I have to use this returned "list" of type double as follows:

double[] date = (DateTime)item; // ERROR Cannot convert type 'double' to 'System.DateTime'

double[] AirTemp = (double)item[1]; //

The above gives me error. I am confused:( what to do. There must be much better and elegant way to do this.

the Web_learner
 
J

Jon Skeet [C# MVP]

Web learner said:
The following code works fine

private List<double> GetDataFor(string column, int selectedYear) {
-------
-------
return list;
}

foreach (double item in GetDataFor("AirTemp", selectedYear)) {
Response.Write(item.ToString() + ", ");
}

It shows data as follows:

37987, -2.42, 37988, -2.41, 37989, -3.34,....................................

where the five digit numbers correspond to date (the first one for 01
Jan 2004) and the decimal negative number as AirTemp. As you can see
both are of type double.

Hmm - do you *have* to return them as a list of doubles? It doesn't
feel like it's the right way of doing things. Anyway.
I have to use this returned "list" of type double as follows:

double[] date = (DateTime)item; // ERROR Cannot convert type 'double' to 'System.DateTime'

double[] AirTemp = (double)item[1]; //

Why are you declaring the variables to be arrays of doubles? I would
expect you'd want:

double[] list = GetDataFor("AirTemp", selectedYear);
for (int i=0; i < list.Length; i+=2)
{
DateTime date = DateTime.FromOADate(list);
double airTemp = list[i+1];
}
 
W

Web learner

That is what I wanted.

You showed me the correct and precise way.

Thanks ....!

Jon Skeet said:
Web learner said:
The following code works fine

private List<double> GetDataFor(string column, int selectedYear) {
-------
-------
return list;
}

foreach (double item in GetDataFor("AirTemp", selectedYear)) {
Response.Write(item.ToString() + ", ");
}

It shows data as follows:

37987, -2.42, 37988, -2.41,
37989, -3.34,....................................

where the five digit numbers correspond to date (the first one for 01
Jan 2004) and the decimal negative number as AirTemp. As you can see
both are of type double.

Hmm - do you *have* to return them as a list of doubles? It doesn't
feel like it's the right way of doing things. Anyway.
I have to use this returned "list" of type double as follows:

double[] date = (DateTime)item; // ERROR Cannot convert type 'double' to
'System.DateTime'

double[] AirTemp = (double)item[1]; //

Why are you declaring the variables to be arrays of doubles? I would
expect you'd want:

double[] list = GetDataFor("AirTemp", selectedYear);
for (int i=0; i < list.Length; i+=2)
{
DateTime date = DateTime.FromOADate(list);
double airTemp = list[i+1];
}
 
J

james.curran

Well, Actually the "correct & precise" way would be for GetDataFor() to
return an List of "DateValue" objects defined like:
class DateValue
{
private DateTime oDate;
private double oValue;
public DateValue(DateTime date, double value)
{
oDate=date;
oValue = value;
}
public DateTime Date
{
get
{
return oDate;
}
}
// Add Similar property getter for Value
}

Then you can read the data as:

foreach (DateValue item in GetDataFor("AirTemp", selectedYear))
{
Response.WriteLine("{0} -- {1}", item.Date, item.Value);
}
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top