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?

Re: Better way to code this? 80 80

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?

Re: Better way to code this? 80 80

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() + " = " +
Re: Better way to code this? 80 80

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.

Re: Better way to code this? 80 80

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


Re: Better way to code this? 80 80

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.