0

I've been practicing with asp.net web forms and controls and have now become a bit stuck on the best way to work with numeric data in user input forms for instance a simple example would be a input field called 'length' and one called width the user clicks a button and the result of L * W is calculated and display.

I have tested converting strings to int using Convert.ToInt32(strvalue2); and out-putting the results to the Console ok.

But when i try and convert them and output them to a form control using something like this:

        string strvaluel = length.Text;
        string strvalueW = width.Text;


            int numValuel = Convert.ToInt32(strvaluel);
            int numValueW = Convert.ToInt32(strvalueW);
            outputbox.Text   = numValuel * numValueW;  (error cannot explicitly convert    type 'int' to 'string'

I'm very new to asp.net so any help would be appreciated

1
  • 1
    On a side note, you should use int.TryParse() to convert a string to an integer when the string is user supplied, so that you can gracefully handle the case where they enter a non-integer value. Commented Jun 7, 2012 at 16:22

1 Answer 1

6

Convert it to a string:

outputbox.Text = (numValuel * numValueW).ToString(); 

If you want more control over formatting (commas, etc.) use a format string:

outputbox.Text = string.Format("{0:N}",numValuel * numValueW); 

See the documentation on format strings for more details

Sign up to request clarification or add additional context in comments.

2 Comments

@D Stanley so does the .ToString(); convert the int's back to strings after the calculation ? many thanks
@Ledgemoneky The result of the calculation is converted, not the arguments. It's the equivalent of int i = numValuel * numValueW; outputbox.Text = i.ToString();. The original values are unchanged, if that's what you're asking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.