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.

Steps

  1. 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. 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.
    Advertisement
  3. 3
    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.
  4. 4
    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.
  5. 5
    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<c>.
  6. 6
    Work with the result. Add the variable C back into other equations for more functionality within the program.</c>
  7. Advertisement

Community Q&A

  • Question
    How do I design a local calculator in VB?
    Gerassimos Moutafis
    Gerassimos Moutafis
    Community Answer
    Add 3 text boxes. 1. number_1. 2. number_2. 3. answer. Add the numbers on buttons(1,2,3,4,5,6,7,8,9,0). Add the + - * = symbols. Write the code with this logic: answer.text=number_1+ number_2.text
Advertisement

About This Article

wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, 9 people, some anonymous, worked to edit and improve it over time. This article has been viewed 56,304 times.
How helpful is this?
Co-authors: 9
Updated: November 16, 2021
Views: 56,304
Categories: Programming
Advertisement