Monday, March 26, 2012

Performing multiplication in web form??

I need to multiply two numbers together on a web form and I have no idea how to do that. Here is the code that I wrote to try to perform this calculation:

if (txtQuantity.Text != null)
{
lblPrice.Text = intPrice * intQuantity;
}

I'm sure this is simple enough, but I'm new to ASP.NET and C# and not sure how this is supposed to work. Both values are coming from, as you can see, txtQuantity.text and lblPrice.text, both of which are strings. I found a couple of explanations about performing calculations online, but it was clearly written for more advanced users. A code sample would be great!

Take care


if (txtQuantity.Text != null)
{
lblPrice.Text = (intPrice * intQuantity).ToString();
}

How do I put txtQuantity.text and lblPrice.text into the variables. I just tried and I get an "cannot convert string to int" error.

Take care


long q=int.Parse(txtQuantity.Text);
long p=int.Parse(txtPrice.Text);

Note that int.Parse() will throw an exception if txtQuantity.Text is, say, "Fred" or "Hi" rather than something that can be parsed as a number.

0 comments:

Post a Comment