User input with numeric variables

This addition program uses text boxes for input and a label to display the result. The following is the event handler for clicking on the Add button:

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        ' Declare the variables
        Dim intIn1 As Integer
        Dim intIn2 As Integer
        Dim intTotal As Integer

        ' Compute and display the result
        intIn1 = CInt(txtIn1.Text)
        intIn2 = CInt(txtIn2.Text)
        intTotal = intIn1 + intIn2
        lblOut.Text = CStr(intTotal) 'Display the result

    End Sub
The first three lines declare three integer variables, intIn1, intIn2 and intTotal.

The next two lines assign the values the user enters to the variables the programmer has set aside for input values. (The user enters the values into TextBoxes called txtIn1 and txtIn2). Note that the CInt function converts the data type of the input from string to integer.

The final statements add the two numbers and assign the result to the Text property of the output label.

Note that the comments on the last statements are at the end of the line. Like comments that are on their own lines, these comments are only for the benefit of a person reading the program. They are ignored during execution.


Disclaimer: The views and opinions expressed on unofficial pages of California State University, Dominguez Hills faculty, staff or students are strictly those of the page authors. The content of these pages has not been reviewed or approved by California State University, Dominguez Hills.