Add Two Numbers in Visual Basic.NET

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. Adding two numbers together isn't complicated, but you have to know how Visual Basic works. Here are the steps you can use to add two numbers in Visual Basic using any version of this programming language.

Steps

  1. Set up the program to recognize the two numbers as values. Programmers can do this either by defining the numbers as constants or variables. Variables are desired over constants for many reasons, mainly because they can be changed easily. For example, a variable can be changed by a user entering a number in a visual text box, whereas a constant cannot.
    • Defining items in Visual Basic requires a "dimension" command, abbreviated as "dim". To define your two 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 two numbers.
  2. Identify your numbers. After dimensioning the two 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.
  3. Create another variable for the sum. Write this code into the same pre-functional prefix code: dim C as integer
  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. Provide for the display of results. You can include a visual text box to display the sum and create a command like: textbox1.text = val<c>
  6. Work with the result. Add the variable C back into other equations for more functionality within the program. There are many ways that programmers can take advantage of an added number to further influence outcomes within an executable program.
    • Run the program to catch bugs. Sometimes, small errors can produce bugs. Run the program and any available diagnostics to make sure the program works and values the sum correctly.
  7. We can define new variable as Dim Sum As Integer
    Sum=Val(Textbox1.text)+Val(Textbox2.text)
    textbox3.text=Sum



Tips

  • Experts have shown that, in addition to the above simple operation, more sophisticated addition in Visual Basic can bypass common elements of this task. The basic idea is to create not 1, but 3, text boxes, entering a command like: textbox3.text = val(textbox1.text) + val(textbox2.text).

Related Articles

  • Add Two Numbers in Visual Basic
  • Write a Vb 2008 Code for Date and Time Run in Realtime
  • Use a Microsoft Data Access Object Comfortably in Vb 6.0
  • Protect VBA Code
  • Call a Function in VB
  • Add a Text Box in Visual Basic

Sources and Citations

  • www.devdos.com/vb/lesson2.shtml
  • support.microsoft.com/kb/148317