下表显示了VB.Net支持的所有比较运算符。 假设变量A保持10,变量B保持20,则:
运算符 | 描述 | 例 |
---|---|---|
= | 检查两个操作数的值是否相等; 如果是,则条件变为真。 | (A = B)是不正确的。 |
<> |
检查两个操作数的值是否相等; 如果值不相等,则条件为真。 | (A<>B)为真。 |
> | 检查左操作数的值是否大于右操作数的值; 如果是,则条件变为真。 | (A> B)是不正确的。 |
< | 检查左操作数的值是否小于右操作数的值; 如果是,则条件变为真。 | (A <B)为真。 |
> = | 检查左操作数的值是否大于或等于右操作数的值; 如果是,则条件变为真。 | (A> = B)是不正确的。 |
<= | 检查左操作数的值是否小于或等于右操作数的值; 如果是,则条件变为真。 | (A <= B)为真。 |
除了上述情况外,VB.Net提供了三个比较运算符,我们将在以后的章节中使用;
然而,我们在这里给出一个简短的描述。
1、Is运算符 -
它比较两个对象引用变量,并确定两个对象引用是否引用相同的对象,而不执行值比较。
如果object1和object2都引用完全相同的对象实例,则result为True; 否则,result为False。
2、IsNot运算符 -
它还比较两个对象引用变量,并确定两个对象引用是否引用不同的对象。 如果object1和object2都引用完全相同的对象实例,则result为False;
否则,result为True。
3、Like运算符 -
它将字符串与模式进行比较。
尝试以下示例来了解VB.Net中提供的所有关系运算符:
Dim a As Integer = 21 Dim b As Integer = 10 If (a = b) Then Proj.MsgDebug.Add("Line 1 - a is equal to b") Else Proj.MsgDebug.Add("Line 1 - a is not equal to b") End If If (a < b) Then Proj.MsgDebug.Add("Line 2 - a is less than b") Else Proj.MsgDebug.Add("Line 2 - a is not less than b") End If If (a > b) Then Proj.MsgDebug.Add("Line 3 - a is greater than b") Else Proj.MsgDebug.Add("Line 3 - a is not greater than b") End If '更换a和b的值 a = 5 b = 20 If (a <= b) Then Proj.MsgDebug.Add("Line 4 - a is either less than or equal to b") End If If (b >= a) Then Proj.MsgDebug.Add("Line 5 - b is either greater than or equal to b") End If '返回结果:Line 1 - a Is Not equal To b '返回结果:Line 2 - a Is Not less than b '返回结果:Line 3 - a Is greater than b '返回结果:Line 4 - a Is either less than Or equal To b '返回结果:Line 5 - b Is either greater than Or equal To b |