Solved

Cannot implicitly convert type 'string' to 'double'

Posted on 2011-02-24
6
972 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

16 Experts available now in Live!

Get 1:1 Help Now