VB.Net - 比较运算符

2018-12-16 13:44 更新

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

运算符描述示例
=Checks if the values of two operands are equal or not; if yes, then condition becomes true.
检查两个操作数的值是否相等; 如果是,则条件变为真。
(A = B) is not true.
<>Checks if the values of two operands are equal or not; if values are not equal, then condition becomes true.
检查两个操作数的值是否相等; 如果值不相等,则条件为真。
(A <> B) is true.
>Checks if the value of left operand is greater than the value of right operand; if yes, then condition becomes true.
检查左操作数的值是否大于右操作数的值; 如果是,则条件变为真。
(A > B) is not true.
<Checks if the value of left operand is less than the value of right operand; if yes, then condition becomes true.
检查左操作数的值是否小于右操作数的值; 如果是,则条件变为真。
(A < B) is true.
>=Checks if the value of left operand is greater than or equal to the value of right operand; if yes, then condition becomes true.
检查左操作数的值是否大于或等于右操作数的值; 如果是,则条件变为真。
(A >= B) is not true.
<=Checks if the value of left operand is less than or equal to the value of right operand; if yes, then condition becomes true.
检查左操作数的值是否小于或等于右操作数的值; 如果是,则条件变为真。
(A <= B) is true.

尝试以下示例来了解VB.Net中提供的所有关系运算符:

Module 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

当上述代码被编译和执行时,它产生以下结果:

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提供了三个比较运算符,我们将在以后的章节中使用; 不过,我们在这里给出一个简短的描述。


1、Is 运算符 -它比较两个对象引用变量,并确定两个对象引用是否引用相同的对象,而不执行值比较。 如果object1和object2都引用完全相同的对象实例,则result为True; 否则,result为False。


2、IsNott 运算符 - 它还比较两个对象引用变量,并确定两个对象引用是否引用不同的对象。 如果object1和object2都引用完全相同的对象实例,则result为False; 否则,result为True。


3、Like 运算符 - 它将字符串与模式进行比较。


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号