How to Add Two Numbers in Visual Basic

Define the 2 numbers to be added as constants or variables., Identify your numbers., Dimension your sum., Write the code needed to identify the third number as the sum of the first 2., Provide for display of results., Work with the result.

6 Steps 2 min read Medium

Step-by-Step Guide

  1. Step 1: 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.
  2. Step 2: 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. , You'll need to create another variable for the sum.

    Write this code into the same pre-functional prefix code: dim C as integer. , With the above example, your code is this:
    C = A + B. , You can include a visual text box on the program to display the sum and create a command like: textbox1.text = val<C>. , Add the variable C back into other equations for more functionality within the program.
  3. Step 3: Dimension your sum.

  4. Step 4: Write the code needed to identify the third number as the sum of the first 2.

  5. Step 5: Provide for display of results.

  6. Step 6: Work with the result.

Detailed Guide

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.

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. , You'll need to create another variable for the sum.

Write this code into the same pre-functional prefix code: dim C as integer. , With the above example, your code is this:
C = A + B. , You can include a visual text box on the program to display the sum and create a command like: textbox1.text = val<C>. , Add the variable C back into other equations for more functionality within the program.

About the Author

D

Deborah Perez

A passionate writer with expertise in hobbies topics. Loves sharing practical knowledge.

45 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: