Solved

Cannot implicitly convert type 'string' to 'double'

Posted on 2011-02-24
6
978 Views
Last Modified: 2012-05-11
Hi, I'm new to c# and am trying to convert the following code:
Thanks!

private void Sum()
    {
        double Price1 = 0;
        double Price2 = 0;

if (txtItemPrice1.Text == "")  
        {
            Price1 = 0.0;
        }
        else
        {
            Price1 = txtItemPrice1.Text; <----
        }

        if (txtItemPrice2.Text == "")
        {
            Price2 = 0.0;
        }
        else
        {    
            Price2 = txtItemPrice2.Text; <---
        }

      Error Cannot implicitly convert type 'string' to 'double'

if (IsNumeric(txtTax.Text) == false)  <----
        {
            txtTax.Text = 0.0;
        }
        else if (txtTax.Text == "")
        {
            txtTax.Text = 0.0;
        }      

TotalAmt = FormatNumber(Price1 + Price2 + txtTax.Text);      
 txtItemPriceAmt.Text = FormatNumber(TotalAmt, 2);  

Error The name 'IsNumeric' does not exist in the current context
0
Comment
Question by:avi7
6 Comments
 
LVL 8

Expert Comment

by:crysallus
ID: 34976598
conversions in C# are normally done using the Convert class, such as

Price2 = Convert.ToDouble(txtItemPrice2.Text);

Here's the link for all the function's available:

http://msdn.microsoft.com/en-us/library/system.convert%28v=VS.100%29.aspx

To check whether a text string can be converted to a number, use the TryParse methods of one the classes representing the base data type that your number could be in. For example, if you want to test whether the text can be converted to a double, use

if (Double.TryParse(txtTax.Text, out result) == false)
...

http://msdn.microsoft.com/en-us/library/994c0zb1.aspx
0
 
LVL 16

Assisted Solution

by:Peter Kwan
Peter Kwan earned 50 total points
ID: 34976606
1) To convert a string to Double, you may use Conver.ToDouble(<your string>)
2) For checking if a string is numeric, you may use "Double.TryParse" method
http://msdn.microsoft.com/en-us/library/994c0zb1.aspx
0
 
LVL 8

Assisted Solution

by:crysallus
crysallus earned 200 total points
ID: 34976621
In fact, your first 2 if else structures could simply be rewritten as:

Double.TryParse(txtItemPrice1.Text, out Price1);
Double.TryParse(txtItemPrice2.Text, out Price2);

because if the text can't be converted to a double, then the double variable is assigned 0 anyway, so there's no need for you to check whether it's empty or not.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

Accepted Solution

by:
dougaug earned 250 total points
ID: 34976632
Try the following code:

private void Sum()
    {
        double Price1 = 0;
        double Price2 = 0;
        double Tax;

        if (txtItemPrice1.Text == "")  
        {
            Price1 = 0.0;
        }
        else
        {
            Price1 = Double.Parse(txtItemPrice1.Text);
        }

        if (txtItemPrice2.Text == "")
        {
            Price2 = 0.0;
        }
        else
        {    
            Price2 = Double.Parse(txtItemPrice2.Text);
        }

        if (Double.TryParse(txtTax.Text, out Tax) == false)  
        {
            txtTax.Text = 0.0;
        }

TotalAmt = FormatNumber(Price1 + Price2 + txtTax.Text);      
 txtItemPriceAmt.Text = FormatNumber(TotalAmt, 2);  

0
 
LVL 8

Expert Comment

by:crysallus
ID: 34976651
TryParse doesn't throw exceptions, Parse does. If you are going to use Parse as dougang suggested, you would have to surround those calls with a try-catch block. Using TryParse avoids the need for exception handling.
0
 

Author Comment

by:avi7
ID: 34976818
Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now