By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,978 Members | 1,019 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,978 IT Pros & Developers. It's quick & easy.

from List <double> to double[]

P: n/a
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
Apr 25 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Web learner <be******@learning.edu> wrote:
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[i]);
double airTemp = list[i+1];
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 25 '06 #2

P: n/a
That is what I wanted.

You showed me the correct and precise way.

Thanks ....!

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Web learner <be******@learning.edu> wrote:
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[i]);
double airTemp = list[i+1];
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Apr 25 '06 #3

P: n/a
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);
}

Apr 26 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.