The Microsoft visual programming environment, known as Visual Basic, has a specific type of clear syntax for adding numbers and doing other operations with integers and other valuations. If you are a beginner who is considering some of the simplest computational tasks in Visual Basic, you may be wondering about how to add 2 numbers together. This task is not extremely complicated, but in order to do it, you do have to know how Visual Basic works. For any version of this programming language, these are the steps that you can use to add 2 numbers in Visual Basic.
Define the 2 numbers to be added as constants or variables.
In order to add 2 numbers in Visual Basic, you need to first set up the program to recognize them as values. Programmers can do this either by defining the numbers as constants or variables. Variables are much desired over constants for many reasons, mainly because they can be changed throughout the program’s implementation. For example, a variable can be changed by a user entering a number in a visual text box, where a constant cannot.
- Dimension constants or variables. To define items in Visual Basic requires a “dimension” command, abbreviated as “dim.” To define your 2 numbers as integers, write the following code “above the fold,” in the initial load sequence before functions are described: dim A as integer, dim B as integer. Here, A and B will be your 2 numbers.
Identify your numbers.
After dimensioning the 2 numbers, you’ll need to either enter values for them in code, or provide instructions for users to populate them during the program. A simple command like A = 5 is sufficient.
Dimension your sum.
You’ll need to create another variable for the sum. Write this code into the same pre-functional prefix code: dim C as integer.
Write the code needed to identify the third number as the sum of the first 2.
With the above example, your code is this: C = A + B.
Provide for display of results.
You can include a visual text box on the program to display the sum and create a command like: textbox1.text = val.
Work with the result.
Add the variable C back into other equations for more functionality within the program.
- Experts have shown that in addition to the above simple operation, more sophisticated addition in Visual Basic can bypass common elements of this task entirely. The basic idea is to create not 1, but 3 text boxes, with a command like: textbox3.text = val(textbox1.text) + val(textbox2.text).