All,

How do I convert a DataTable double from a row object to a normal double?

I'm creating the DataTable like this (note that Temp and Hum are doubles):

private void readDummyData(DataTable dt)
        {
            string fileName = "dummydata.csv";
            string[] lineCSV = new string[5];
            StreamReader myReader = new StreamReader(fileName);

            // Define data table
            dt.Columns.Add("TimeStamp", Type.GetType("System.DateTime"));
            dt.Columns.Add("Location", Type.GetType("System.String"));
            dt.Columns.Add("Temp", Type.GetType("System.Double"));
            dt.Columns.Add("Hum", Type.GetType("System.Double"));
            dt.Columns.Add("Status", Type.GetType("System.String"));

            string line;
            while ((line = myReader.ReadLine()) != null)
            {
                lineCSV = line.Split(new char[] { ',' });
                DataRow dr = dt.NewRow();
                dr["TimeStamp"] = lineCSV[0];
                dr["Location"] = lineCSV[1];
                dr["Temp"] = lineCSV[2];
                dr["Hum"] = lineCSV[3];
                dr["Status"] = lineCSV[4];
                dt.Rows.Add(dr);
            }
            myReader.Close();
        }

And I'm trying to use the data like this:

// Build X and Y arrays
            int i = 0;
            foreach (DataRow row in dataTable.Rows)
            {
                xData[i] = i;
                tempData[i] = double.Parse(row[2] + "");
                humData[i] = double.Parse(row[3] + "");               
                tempLowerLimitData[i] = tempMin;
                tempUpperLimitData[i] = tempMax;
                humLowerLimitData[i] = humMin;
                humUpperLimitData[i] = humMax;
                i++;
            }

Note the funky double.Parse(row[2] + ""). That's the only way I could turn the row object into a string so that I could double.Parse it. (row[2].ToString() doesn't work.)

row[2] and row[3] are already doubles, but when I do this tempData[i] = row[2] I get the following error meaasge: Error 1 Cannot implicitly convert type 'object' to 'double'. An explicit conversion exists (are you missing a cast?)

It says that an explicit version exists, but I can't figure out what that is.

Anyone have an answer?

Thanks,
-Bill

row[2] and row[3] are already doubles

In a DataRow, they're objects. They may actually be doubles, but the immediate representation is Object as stated by the documentation for the index operator, so you must do a conversion.

Your solution is funky because double.Parse() requires a string argument. You can make it less funky by using Convert.ToDouble(), which has an overload for Object:

tempdata[i] = Convert.ToDouble(row[2]);

You can make it less funky by using Convert.ToDouble(), which has an overload for Object:

Wow. That works great and is a lot cleaner. Thanks. This is touching on a pet peave of mine--why is there more than one way to do something? I saw Convert.ToDouble() but didn't try it because I figured if double.Parse didn't work neither would that.

In this age of overloading to the nth degree, why is it necessary to have one method that converts strings to double and another that converts objects? Why can't it all be done with one method and one method only? That would certainly save a lot of confusion.

Thanks again,
-Bill

why is there more than one way to do something?

In any language that's suitably powerful to be called general purpose, it's pretty much impossible to avoid multiple ways of accomplishing something.

I guess that's true. Good point.

Thanks again for your help. Case closed.

-Bill

This question has already been answered. Start a new discussion instead.