Converting string to int c# .NET

Associate
Joined
7 Sep 2007
Posts
400
Location
Edinburgh
I am pulling a price (real) from a SQL database and putting it into a text box, when I press the buy button.

TextBox1.Text = dr["Shirt_Price"].ToString();
TextBox2.Text = dr["Shoe_Price"].ToString();

I however want to add the two text box's together to give a total cost.

I have tried,

TextBox1.Text = Convert.ToInt32, but I can't get it to work.

I am still learning C#, so if anycome could put me in the right direction it would be great.
 
I think you do Convert.ToInt32(TextBox1.Text) - cant be sure as ive never written any c# but thats how you do it in c++ .net.

int finalVal = Convert.ToInt32(TextBox1.Text) + Convert.ToInt32(TextBox2.Text)
 
Last edited:
so make an int for instance

int text01 = 0;

text01 = System.Convert.ToInt32(textbox1.text);


then add the two


that should work :)
 
You can't explicitly cast a string to an int so you can use int.Parse(textbox1.Text) or Convert.ToInt32(textbox1.Text).

EDIT: textBox3.Text = int.Parse(dr["Shirt_Price"].ToString()) + int.Parse(dr["Shoe_Price"].ToString());

EDIT2: Or if the data in the rows are integer values anyway, you can do a direct cast. textBox3.Text = (int)dr["Shirt_Price"] + (int)dr["Shoe_Price"];
 
Last edited:
Just as an aside, you can also use int.TryParse to test for validity without risking an exception being thrown, if you expect input to be erroneous.
 
The value is passed back through an out parameter to the method. You can do something like this.

Code:
int shirtPrice = 0;
int shoePrice = 0;
int totalPrice = 0;

if (int.TryParse(dr["Shirt_Price"].ToString(), out shirtPrice) && shirtPrice > 0)
{ 
   // shirt price will have been set
   totalPrice = shirtPrice;
}

if (int.TryParse(dr["Shoe_Price"].ToString(), out shoePrice) && shoePrice > 0)
{ 
   // shoe price will have been set
   totalPrice += shoePrice;
}

textBox3.Text = totalPrice.ToString();
 
Last edited:
Only a round numbered price will fit into an int I would suggest thats your main issue

That's a very valid point - int's will only hold whole numbers (e.g. 1, 60, 100 etc). I'd use a double, as these will hold decimal values no problem (stay away from floats as these are imprecise).
 
That's a very valid point - int's will only hold whole numbers (e.g. 1, 60, 100 etc). I'd use a double, as these will hold decimal values no problem (stay away from floats as these are imprecise).

Isn't Double a floating point datatype and therefore still going to allow imprecise values, even when adding two simple numbers together that are restricted to 2 decimal places. If you're dealing with money amounts, the Decimal datatype is probably more appropriate.
 
Isn't Double a floating point datatype and therefore still going to allow imprecise values, even when adding two simple numbers together that are restricted to 2 decimal places. If you're dealing with money amounts, the Decimal datatype is probably more appropriate.

Decimals are also floating points, only they're base 10, rather than base 2, meaning they can exactly represent numbers that have finite numeric expansions in base 10 that wouldn't in base 2. They're not inherently more precise than normal floating points, but they do provide greater precision when you're dealing with numbers that are suited to base 10 representation (e.g. anything involving money).

Edit: here's a couple of articles on the decimal data type and on binary floating points:

http://www.yoda.arachsys.com/csharp/decimal.html
http://www.yoda.arachsys.com/csharp/floatingpoint.html
 
Last edited:
Decimals are also floating points, only they're base 10, rather than base 2, meaning they can exactly represent numbers that have finite numeric expansions in base 10 that wouldn't in base 2. They're not inherently more precise than normal floating points, but they do provide greater precision when you're dealing with numbers that are suited to base 10 representation (e.g. anything involving money).

Thanks for the information. Learn something new everyday :)
 
Back
Top Bottom