from List <double> to double[]

Discussion in 'ASP .Net' started by Web learner, Apr 25, 2006.

  1. Web learner

    Web learner Guest

    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, Apr 25, 2006
    #1
    1. Advertising

  2. Web learner <> 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);
    double airTemp = list[i+1];
    }

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too
     
    Jon Skeet [C# MVP], Apr 25, 2006
    #2
    1. Advertising

  3. Web learner

    Web learner Guest

    That is what I wanted.

    You showed me the correct and precise way.

    Thanks ....!

    "Jon Skeet [C# MVP]" <> wrote in message
    news:...
    > Web learner <> 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);
    > double airTemp = list[i+1];
    > }
    >
    > --
    > Jon Skeet - <>
    > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    > If replying to the group, please do not mail me too
     
    Web learner, Apr 25, 2006
    #3
  4. Web learner

    Guest

    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, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sb
    Replies:
    4
    Views:
    314
    Alberto Barbati
    Feb 19, 2004
  2. Jacek Dziedzic
    Replies:
    5
    Views:
    392
    Old Wolf
    Apr 8, 2004
  3. ferran
    Replies:
    9
    Views:
    3,062
    Kevin Goodsell
    Apr 12, 2004
  4. da Vinci
    Replies:
    5
    Views:
    8,749
    da Vinci
    Jul 31, 2004
  5. Sydex
    Replies:
    12
    Views:
    6,564
    Victor Bazarov
    Feb 17, 2005
Loading...

Share This Page