Tutorial 03: Adding Two Numbers

May 01, 2008

© NSB Corporation. All rights reserved.

Contributed by Dynamite Inc


Contents:

  1. Getting Behind the Scenes
  2. Creating the Code
  3. Adding Together User Input
  4. "Crunching the Numbers"
  5. Handling Decimals

1. Getting Behind the Scenes

To demonstrate simply adding two numbers toegether, we will first go to the "After Code" of the project's only form. Right-click and select "After Code"

2. Creating the Code

Now we will declare three variable, all as integers, which whole numbers

  1. firstnumber as an Integer, which of course, will contain the value of half our math equation
  2. secondnumber as an Integer as well
  3. total as an Integer, which will contain the answer

Next we set firstnumber to equal 10 and secondnumber to 7

To set total as the sum of both of these is quite simple; just include + between the two variable names that are to added together. You're done!

To test and make sure it worked, we will display a message box with a string representation of the Integer total by using the Str() function.


3. Adding Together User Input

Now we will work on adding together two values that is inputted by the user in two different text fields. In our example, it will be firstnumberfield and secondnumberfield

The first step here is to create three text fields. Name each accordingly, and also change the attribute of Numeric to "True" for each. This will ensure that only numbers are entered.

The third and last field, totalfield, we will set Editable to "False" to keep the user from changing the answer, and also set Underline to "False" so that it does not confuse them to think they can change it.

Lastly, we will create a button and change the label to "Add!"

4. "Crunching the Numbers"

The next step is to actually perform the calculation when the user taps on the "Add!" button. Double-click on the button to bring up the code window for that action.

The only variable we need to declare here will be the answer, as the numbers used will be directly read from the text field inputs.

In order to deal with the inputs properly, they need to be evaluated as numbers and not as strings, which is how the value of text fields are stored in the object. Using the val() function, we can convert the string inputs for each of the fields. We will reference the value of the fields using the ".text" attribute of the text field object.

Lastly, we assign ".text" attribute of the totalfield object as a string representation of our theanswer integer


5. Handling Decimals

You may soon find out that your simple calculator cannot correctly add any numbers with a decimal place.

Luckily, the solution is very simple.

Integer variables can only handle whole numbers. If any decimals are found, they are simply dropped. 9.999 turns into just 9.

To allow for decimals, you must use the variable type of Float.

The functions val() and str() can correctly handle integers or floats, so nothing needs to change in your code expect the declration ("Dim") statement.