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)) + " = " +

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

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?

1 Year
Discussion Span
Last Post by buzzstpoint

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() + " = " +

Edited 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?


Edited 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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.