Justin
Justin

Reputation: 275

How do I convert int to a double?

I am having trouble converting my int values to doubles. I know I have to cast them, and I am trying to do that; but C# still gives me an error for converting int to double and says I need a cast. Also It gives me an error for trying to obtain the value for totalunits under values. it says: "**A local variable named 'values'cannot be declared in this scope because it would give a different meaning to 'values', which is already used in a parent or current scope to denote some else. Im confused on this error because I want totalunits to change values as well. Can somweone help me? Thank you.

Justin

Here is some of my code:

//Get the Refurb_Rate for the day


private double GetRefurbRate()
{
    string sql = "";
    double Refurb_Rate = 0;
    int totalRefurb = 0;
    int totalUnits = 0;
    string error_msg = "";

        //Getting the value for sql for totalRefurb
        sql = "SELECT COUNT(distinct rp.repair_ord) " +
           "FROM " + schema + ".repair_part rp " +
           "WHERE rp.repair_type = 'REFURB' and rp.created_date > '" + DateTime.Now.ToString("d-MMM-yyyy") + "' ";


        while (true)
        {
            if (!myDb.RunSql(sql, true))
            {
                error_msg = "DBError for getting Refurb Rate";
                break;
            }
            if (myDb.dbRdr.HasRows)
            {
                if (myDb.dbRdr.Read())
                {


                    object[] values = new object[myDb.dbRdr.FieldCount];
                    myDb.dbRdr.GetValues(values);
                    Console.WriteLine(values[0].ToString());
                    totalRefurb = Convert.ToDouble(values[0].ToString());());// This is where convert to double error comes in.


                    //Getting the value from sql for totalUnits
                    sql = "SELECT count(distinct rp.repair_ord) " +
                        "FROM " + schema + ".repair_part rp " +
                        "WHERE rp.repair_type = 'REFURB' and rp.ref_desig is null  and rp.created_date > '" + DateTime.Now.ToString("d-MMM-yyyy") + "' ";

                    while (true)
                    {
                        if (!myDb.RunSql(sql, true))
                        {
                            error_msg = "DBError for getting Refurb Rate";
                            break;
                        }
                        if (myDb.dbRdr.HasRows)
                        {
                            if (myDb.dbRdr.Read())
                            {

                                // This is where the values error comes in
                                object[] values = new object[myDb.dbRdr.FieldCount];
                                myDb.dbRdr.GetValues(values);
                                Console.WriteLine(values[1].ToString());
                                totalUnits = Convert.ToDouble(values[1].ToString());// This is where convert to double error comes in.

                                try
                                {

                                    //Formula for Refurb Rate
                                    Refurb_Rate = totalRefurb / totalUnits * 100;


                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine(e);
                                }

                            }

                        }

                        break;
                    }
                    myDb.dbRdr.Close();

                    if (error_msg != String.Empty)
                    {
                        MessageBox.Show(error_msg, "Get Refurb Rate",
                                        MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }

                    return Refurb_Rate;
                }
            }
        }
    }

Upvotes: 1

Views: 14004

Answers (6)

Jeffrey L Whitledge
Jeffrey L Whitledge

Reputation: 59443

You aren't trying to convert an int to double in that line; the result of ToString() is a string, not an int. If the object in values[1] is an int, then do this instead:

totalUnits = (double)(int)values[1];

Of course, this works better if totalUnits is actually declared to be a double rather than an int.

Regarding the other error

A local variable named 'values'cannot be declared in this scope because it would give a different meaning to 'values', which is already used in a parent or current scope to denote some else.

This is a tricky error that gives a lot of people problems. The real problem is that a local variable named 'values' cannot be declared in this scope because it would give a different meaning to 'values', which is already used in a parent scope to denote something else. If you deal with that problem, then the compile error goes away.

In other words, "values" refers to the variable that is declared about one-third the way down the code. You are attempting to create another variable named "values" at just over half-way through the code. If that second declaration were allowed, then the same name ("values") would refer to two different things inside this method.

Upvotes: 2

Jon Raynor
Jon Raynor

Reputation: 84

The second object[] values =

Should be

values =
OR object[] values2 =

The int to double conversion:

int i = 123; double dub;

dub = (double)i;

What's the datatype that is coming from the database?

Convert the DB datatype to a .Net one and then do the cast (if possible).

For example(s), a SQL Server nvarchar column is string, int is Int32 and bigint is Int64.

int i = (int)myDb.dbRdr["IntColumnName"]; //Might want to do a null check if allow null values.

double d = (double)i;

Upvotes: 0

Shadow Wizard
Shadow Wizard

Reputation: 66389

  1. totalRefurb need to be declared a double:
    double totalRefurb = 0;
  2. values problem already been answered.

Upvotes: 2

anon
anon

Reputation:

The problem is that you declaring your variables values a second time. This is not possible, since in the same scope you have already the same variable. Therefore in the second case just use:

values = new object[myDb.dbRdr.FieldCount];

Upvotes: 0

JSBձոգչ
JSBձոգչ

Reputation: 41378

Your problem has nothing to do with casting. It's because you've used two different variables named values:

object[] values = new object[myDb.dbRdr.FieldCount];
// ... snipped ...

// This is where the values error comes in
object[] values = new object[myDb.dbRdr.FieldCount];

Upvotes: 2

Albin Sunnanbo
Albin Sunnanbo

Reputation: 47038

Change the second object[] values = new object[myDb.dbRdr.FieldCount];
to values = new object[myDb.dbRdr.FieldCount];
or object[] values2 = new object[myDb.dbRdr.FieldCount];

The first if you want to replace the content of values, the second if you want to declare an independent variable..

Upvotes: 4

Related Questions