VB.Net比较运算符

下表显示了VB.Net支持的所有比较运算符。假设变量A=10,变量B=20,则:

运算符 描述 说明
== 检查两个操作数的值是否相等; 如果是,那么条件为True (A == B)结果为:False
<> 检查两个操作数的值是否相等; 如果值不相等,则条件为True (A <> B)结果为:True
> 检查左操作数的值是否大于右操作数的值; 如果是,则条件为True (A > B)结果为:False
< 检查左操作数的值是否小于右操作数的值; 如果是,则条件为True (A < B)结果为:True
>= 检查左操作数的值是否大于等于右操作数的值; 如果是,则条件为True (A >= B)结果为:False
<= 检查左操作数的值是否小于等于右操作数的值; 如果是,则条件为True (A <= B)结果为:True

除此之外,VB.Net还提供了三个比较运算符,我们将在以后的章节中使用它们。 但是,在这里给出一个简短的描述。

  • Is运算符 - 它比较两个对象引用变量,并确定两个对象引用是否引用同一个对象而不执行值比较。 如果object1object2都引用完全相同的对象实例,则结果为True; 否则,结果为False
  • IsNot运算符 - 它还比较两个对象引用变量,并确定两个对象引用是否引用不同的对象。 如果object1object2都引用完全相同的对象实例,则结果为False; 否则,结果为True
  • Like运算符 - 它将字符串与模式进行比较。

示例

尝试下面的例子来理解VB.Net中可用的所有关系运算符:

Module comparison_operators
   Sub Main()
      Dim a As Integer = 21
      Dim b As Integer = 10
      If (a = b) Then
          Console.WriteLine("Line 1 - a is equal to b")
      Else
          Console.WriteLine("Line 1 - a is not equal to b")
      End If
      If (a < b) Then
          Console.WriteLine("Line 2 - a is less than b")
      Else
          Console.WriteLine("Line 2 - a is not less than b")
      End If
      If (a > b) Then
          Console.WriteLine("Line 3 - a is greater than b")
      Else
          Console.WriteLine("Line 3 - a is not greater than b")
      End If
      ' Lets change value of a and b '
      a = 5
      b = 20
      If (a <= b) Then
          Console.WriteLine("Line 4 - a is either less than or equal to  b")
      End If
      If (b >= a) Then
          Console.WriteLine("Line 5 - b is either greater than  or equal to b")
      End If
      Console.ReadLine()
   End Sub
End Module

执行上面示例代码,得到以下结果 -

F:\worksp\vb.net\operators>vbc comparison_operators.vb
F:\worksp\vb.net\operators>comparison_operators.exe
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

上一篇: VB.Net运算符 下一篇: VB.Net决策结构