VB.Net支援下列賦值運算子:
運算子 | 描述 | 說明 |
---|---|---|
= |
簡單賦值操作符,將右側運算元的值賦給左側運算元 | C = A + B 將把A + B 的值賦值給C |
+= |
新增和賦值操作符,它將右運算元新增到左運算元,並將結果賦給左運算元 | C += A 等效於 C = C + A |
-= |
減去和賦值運算子,從左邊的運算元中減去右邊的運算元,並將結果賦給左邊的運算元 | C -= A 等效於 C = C - A |
*= |
乘法和賦值運算子,它將右運算元與左運算元相乘,並將結果賦給左運算元 | C *= A 等效於 C = C * A |
/= |
除法和賦值運算子,用左運算元和右運算數分開,賦值給左運算元(浮點除法) | C /= A 等效於 C = C / A |
\= |
除法和賦值運算子,用左運算元除以右運算元,並將結果賦給左運算元(整數除法) | C \= A 等效於 C = C \A |
^= |
指數運算和賦值運算子。它將左運算元指定的右運算元指數的冪值,並將結果賦給左運算元。 | C^=A 等效於 C = C ^ A |
<<= |
左移和賦值運算子 | C <<= 2 等效於 C = C << 2 |
>>= |
右移和賦值運算子 | C >>= 2 等效於 C = C >> 2 |
&= |
將一個字串(String)表示式連線到一個字串(String)變數或屬性,並將結果賦給變數或屬性。 | Str1 &= Str2 等效於 Str1 = Str1 & Str2 |
嘗試下面的例子來理解VB.Net中的所有賦值運算子,檔案:assignmentOp.vb
Module assignmentOp
Sub Main()
Dim a As Integer = 21
Dim pow As Integer = 2
Dim str1 As String = "Hello! "
Dim str2 As String = "VB Programmers"
Dim c As Integer
c = a
Console.WriteLine("Line 1 - = Operator Example, Value of c = {0}", c)
c += a
Console.WriteLine("Line 2 - += Operator Example, Value of c = {0}", c)
c -= a
Console.WriteLine("Line 3 - -= Operator Example, Value of c = {0}", c)
c *= a
Console.WriteLine("Line 4 - *= Operator Example, Value of c = {0}", c)
c /= a
Console.WriteLine("Line 5 - /= Operator Example, Value of c = {0}", c)
c = 20
c ^= pow
Console.WriteLine("Line 6 - ^= Operator Example, Value of c = {0}", c)
c <<= 2
Console.WriteLine("Line 7 - <<= Operator Example, Value of c = {0}", c)
c >>= 2
Console.WriteLine("Line 8 - >>= Operator Example, Value of c = {0}", c)
str1 &= str2
Console.WriteLine("Line 9 - &= Operator Example, Value of str1 = {0}", str1)
Console.ReadLine()
End Sub
End Module
執行上面範例程式碼,得到以下結果 -
F:\worksp\vb.net\operators>vbc assignmentOp.vb
F:\worksp\vb.net\operators>assignmentOp.exe
Line 1 - = Operator Example, Value of c = 21
Line 2 - += Operator Example, Value of c = 42
Line 3 - -= Operator Example, Value of c = 21
Line 4 - *= Operator Example, Value of c = 441
Line 5 - /= Operator Example, Value of c = 21
Line 6 - ^= Operator Example, Value of c = 400
Line 7 - <<= Operator Example, Value of c = 1600
Line 8 - >>= Operator Example, Value of c = 400
Line 9 - &= Operator Example, Value of str1 = Hello! VB Programmers