from List <double> to double[]

  • Thread starter Thread starter Web learner
  • Start date Start date
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
 
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];
}
 
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];
}
 
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);
}
 
Back
Top