Integer Math Shorthand
In this Exercise, you’ll see how can perform the same operations without having to write as much code by using shorthand operators (assignment operators). Although they look a little less logical than their more verbose counterparts, you’ll soon learn to love them.
Exercise - Using Shorthand Operators
1. Go back to Visual Studio .net and open the code for Form1.vb again. Change the highlighted lines:
Private Sub btnIntMath_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnIntMath.Click
‘Declare variable
Dim intNumber As Integer
‘Set number, add numbers, and display results
intNumber = 16
intNumber += 8
MessageBox.Show(“Addition test... ” & intNumber, “Integer Math”)
‘Set number, subtract numbers, and display results
intNumber = 24
intNumber -= 2
MessageBox.Show(“Subtraction test... ” & intNumber, “Integer Math”)
‘Set number, multiply numbers, and display results
intNumber = 6
intNumber *= 10
MessageBox.Show(“Multiplication test... ” & intNumber, “Integer Math”)
‘Set number, divide numbers, and display results
intNumber = 12
intNumber /= 6
MessageBox.Show(“Division test... ” & intNumber, “Integer Math”)
End Sub
|
2. Run the project and click the Math Test button. You’ll get the same results as in the previous Exercise.
click next Page - How It Works. |