Solved

Cannot implicitly convert type 'string' to 'double'

Posted on 2011-02-24
6
983 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

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 is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

778 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