Beginning coding here. But in my programming class I have a basic program to enter a couple of numbers, calculates the basic math then displays the equation and answer.

Here is my output to app:

lblAdd2.Text = Convert.ToString(vTools.unformat(txtNum1.Text)) + " + " +
               Convert.ToString(vTools.unformat(txtNum2.Text)) + " = " +
                  mathNum1.ToString();

lblSub2.Text = Convert.ToString(vTools.unformat(txtNum1.Text)) + " - " +
               Convert.ToString(vTools.unformat(txtNum2.Text)) + " = " +
                  mathNum2.ToString();

Is there a way to keep reusing the Conver.ToString(vTools.unformat ? Meaning to shorten a bit to clean up the code a little bit? Possibly type it in once? or leave it as is?

Your code begs the questions: Why convert text to string when it's already text? What does the unformat function do? Also if the unformat function is necessary why isn't it returning a string?

vTools has a couple of handy tools written or put together by an instructor, the unformat takes out symbols. If someone were to put in $4 for num1 instead of 4 the "unformat" removes the dollar sign.

Edit: unformat -
/* This function removes dollar signs and commas from numeric strings.
* If the string is formatted for percent, it removes the percent sign
* and divides the remaining number by 100 to convert it back to a decimal.
*/

I quess i was reading too much in the convert.
I think I simplified it:

                // Inputs to Varibles.
                Num1 = int.Parse(vTools.unformat(txtNum1.Text));
                Num2 = int.Parse(vTools.unformat(txtNum2.Text));

                //Calculating Results.
                mathNum1 = Num1 + Num2;

                // Displaying Results
               lblAdd2.Text = Num1.ToString() + " + " +
                              Num2.ToString() + " = " +
                                mathNum1.ToString();

Edited 1 Year Ago by buzzstpoint: added description of vTools

You could simplify further by putting the display routine into a Sub, so that you don't have to repeat the code for each operation.

Hello buzzstpoint!
Did that code work for you? Did it give you the result you were expecting?

Tekkno

Edited 1 Year Ago by TekknoDraykko

Teckno, yes both codes worked. My 1st attempt just looked messy code and wanted something a little more simplified. My 2nd attempt did work as well so I think that will work for this point in my class.

This question has already been answered. Start a new discussion instead.