Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

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

Posted on 2011-02-24
Medium Priority
1,000 Views
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
Question by:avi7
[X]
###### 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

LVL 8

Expert Comment

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

Peter Kwan earned 200 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

crysallus earned 800 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

LVL 11

Accepted Solution

dougaug earned 1000 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

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

ID: 34976818
Thanks!
0

## Featured Post

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-toâ€¦
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â€¦
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signalâ€¦