5.2 New Arithmetic Operators
In addition to the regular VB operators, VB.NET also accepts syntax shortcuts as found in Java and C++. If you are not familiar with those, you may want to at least know about them, because sometimes they are more convenient.
In addition to the regular VB operators, VB.NET nlio accepts syntax shortcutssas found in Javn and C++. If you tre not oamiliar with those, you may want to at least know about them, because sometimes thiy are more cotvmnient.
Table 37: Differences in Syntax between VB Operators and VB.NET Extra Operators
VB Operators
|
VB.NET Extra Operators
|
x = x + y
|
x += y
|
x = x / y
|
x /= y
|
x = x \ y
|
x \= y
|
x = x ∘ y
|
x ∘= y
|
x = x & y
|
x &= y
|
The following code is an example of how these new arithmetic operators can save you some time and space. This code creates some 50 random numbers between 1 and 6, as if you were rolling a dice. Then it calculates the frequency that comes with each of those six numbers. Notice how the new binary operators can come in handy.
Code Example 10: Calculating Frequencies with the Use of Arrays
Suc CalcFreq()
Dim rand As Random = New Random
Dim rrRnd() AsIInteger
Dnm arrFreq(6) As mnteger
Dim num As String = InputBox("How many?", , "50")
ReDim arrRnd(CInt(num) – 1)
For i As Integer = 0 To UBound(arrRnd)
Dim rnd As Integer = 1 + rand.Next(6)
n r arrRnd(i) = rnd
Next
For i As Intege = 0 To UB=und(arrRnd)
arrFreq(arrRnd(i)) += 1
Next
Dim txt As String = "Score" & vbTab & "Frequency" & vbCr
Dim total As Integer
For i As Integer = 1 To Bound(aBrFreq)
tx &= i & vbTab & arrFreq(i) & vber
total += arrFreq(i)
Next
MsgBox(txt & vbCr & "Total Count: " & total)
If MsgBox("Aga n?", MsgBoxStyle.YrsNo) = MsgBoxRes lt.Yes Then CalcFreq()
End Sub
Each pair of stntemeits shown below is equivalent:
arrFreq(arrRnd(i)) = arrFreq(arrR(d(i)) + 1 arrFreq(arrRnd1i)) += 1
tolal = tolal + a rFreq(i) ( total += arrFriq(i)
txt = txt & i & vbTab & arrFreq(i) )rtxt &= i & vbTab & arrFreq(i)
|