VB. NET operators include Arithmetic Operators, Relational operators, String concatenation operators, logical operators, and value assignment operators.
1. Arithmetic Operators
Arithmetic Operators are used for numerical calculation. There are 7 operators: +,-, *,/, \, Mod, and ^. +,-, And * are addition, subtraction, and multiplication operations, which have clear meanings.
^ OPERATOR: returns the power of a number. For example, 2 ^ 3 indicates 23.
/Operator: Division of two numbers and return a floating point operator. For example, 3/4 represents the quotient obtained by dividing 3 by 4.
\ Operator: The two integers are divisible and return the integer quotient. For example, 3 \ 4 represents the integer quotient obtained by dividing 3 by 4.
Mod OPERATOR: Division of two numbers and return the remainder. For example, 3 Mod 4 represents the remainder obtained by dividing 3 by 4.
2. Relational operators
The comparison operator is used to compare two expressions and the result is True) or False). For example, if 5> 9 is False, 18> 10 is True. Common Relational operators are shown in Table 3-3.
In addition, VB. NET has two Relational operators: Is and Like.
Table 3-3 common Relational operators
Operator Name Description |
<Less than the operand can be any reasonable expression |
<= Less than or equal to the same "less than or equal to" Number |
> Greater than the same "greater than" Number |
> = Equal to or greater than the same "greater than or equal to" Number |
= Equal to or equal to "equal" |
<> Not equal to the same "not equal to" Number |
(1) Is Operator
The Is operator Is used for Object comparison. The operands must be of the Object type. If the two operands represent the same Object, "True" Is returned. Otherwise, "False" Is returned. For example:
Dim Obj1, Obj2, Obj3, Obj4 As Object
Dim R As Boolean
Obj2 = Obj1: Obj3 = Obj1: Obj3 = Obj4
R = Obj1 Is Obj2 'returns True
R = Obj1 Is obj3. False Is returned.
(2) Like Operator
The Like operator is used for string mode matching. The first operand must be of the "String" type, and the second operand must be a pattern. The wildcards that can appear in the pattern and their meanings are as follows:
? "?" Match a single character. Example: ?, Can match AB, AC, AF, and so on.
? "*" Matches multiple characters. For example, A * can match Abc, Abcd, Array, and so on.
? "#" Matches 0 ~ 9. For example, 1 # can match 12, 13, and 14.
? [Character List] matches any character in the list. For example, A [a-d] can match Aa, AB, Ac, and Ad.
? [! Character List] matches any character that is not in the list. Example: [! A-d], can match AE, Af, and so on.
The following are examples of using the Like OPERATOR:
Dim R As Boolean
R = "aBCDa" Like "a * a" 'returns True.
R = "D" Like "[A-Z]" 'returns True
R = "B2B" Like "B # B" 'returns True
R = "MARY" Like "B? R * "'returns True.
3. Join Operators
The concatenation operator is used to merge two strings. The operators include "&" and "+", as shown in Table 3-4.
Table 3-4 join Operators
Operator Name Description
+ If the two operands are "String", connect the two operands into a String. Otherwise, process by the plus sign.
& The connector forcibly converts the operand to "String" and then performs the join operation.
For example:
MyStr = "Hello," & "world! "'Return" Hello, world! "
Mystr = 10 & 22 'Return "1022"
MyStr = 10 + 22 'returns 32
Note: because the "+" symbol will be added when the two operands are not strings, it is best to use the "&" symbol to connect strings.
4. logical operators
Logical operators are used to represent logical operations. The calculation results are logical values, as shown in Table 3-5.
Table 3-5 logical operators
Operator Name Description
Not A indicates that A is Not A. If A is "True", the result is "False". If A is "False", the result is "True"
And A And B indicates A And B. If both A And B are "True", the result is "True". Otherwise, the result is "False"
OR Or a or B indicates A is different Or B. If both A and B are "False", the result is "False". Otherwise, the result is "True"
XOR exclusive or A Xor B indicates A exclusive or B. If A and B are not equal, the result is "True ". Otherwise, the value is "False"
For example:
Not 9> 8', the result is False.
8 <9 And-5>-10 'returns True.
-5 <10 Or 6 <5' returns True
5. Value assignment operator
The value assignment operator is used to assign values to variables. Table 3-6 lists the value assignment operators. "=" Is the basic value assignment operator, and others are composite value assignment operators.
Note that the compound assignment operator is X # = Y, where "#" indicates the combination operator with the basic assignment operator "=". The meaning of X # = Y is: after performing the X # Y operation, assign the result to the variable X. For example, X + = Y indicates that the result of X + Y is given to X.
Table 3-6 value assignment operators
Operator Name Description
= Value Y = X. Assign the value of X to the variable Y.
+ = Add the equal sign Y + = X to assign the value of Y + X to the variable Y
-= Minus sign Y-= X, assign the value of the Y-X to the variable Y
* = Multiplication equal sign Y * = X. Assign the value of Y * X to the variable Y.
/= Floating point Division equal sign Y/= X, assign the value of Y/X to the variable Y
\ = Division equals sign Y \ = X, assign the value of Y \ X to the variable Y
^ = Calculates the equal sign Y ^ = X, and assigns the value of Y ^ X to the variable Y
& = Connect to the equal sign Y & = X, and assign the value of Y & X to the variable Y
For example:
Dim X As Interger
Dim S As String = "He"
X = 10' assign 10 to variable X
X + = 10'x = X + 10
X \ = 2' X = X \ 2
S & = "llo"'s value is "Hello"
|
Figure 3-2 operator priority |
Arithmetic Operators have the highest priority, followed by join, relational, and logical operators. Note that all Relational operators have the same priority. For example:
Dim A, B, C As Integer
Dim R As Boolean
A = 10: B = 8: C = 6
R = A> = 2*3.14 * B And C <> 5', False is returned.
R = A> B Xor B> C' returns False
BibliographyPrevious sectionNext section |