Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Cannot implicitly convert type 'string' to 'double'

Posted on 2011-02-24
6
985 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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