代码之家  ›  专栏  ›  技术社区  ›  Sylvain Rodrigue

VB.NET语言和Nothing:为什么会这样?

  •  7
  • Sylvain Rodrigue  · 技术社区  · 17 年前

    If myObject Is Nothing then
    

    If Nothing Is myObject Then
    

    if myObject = null then
    

    if null = myObject then
    

    if myObject = Nothing Then
    

    编译器不会接受这一点。

    如果没有什么是myObject 远没有那么明显 .

    5 回复  |  直到 17 年前
        1
  •  25
  •   JaredPar    17 年前

    “=”运算符用于值比较。值比较只能用于定义显式=、<>类定义中的运算符对。等式的实际实现取决于运算符的实现。

        2
  •  4
  •   Otávio Décio    17 年前

        3
  •  3
  •   Fredrik Mörk    17 年前

    嗯,在某些情况下,你可以写 If myObject = Nothing Then ,但它不会是空比较:

    Dim someValue As Integer
    If someValue = Nothing Then
        ' do something '
    End If
    

    Dim someValue As Integer
    If someValue = 0 Then
        ' do something '
    End If
    

    Integer Point.Empty 为了一个 Point (检查比较的对象是否为同一实例),而等号为 .

        4
  •  1
  •   JB King    17 年前

    另一个想法是,如果你看看一些循环和条件语法,VB的意图会更详细。举几个例子:

    If x==0 Then
    End If
    
    While 
    End While
    
    For i = 0 to 10 Step 2
    Next i
    

     If x==0 
     {
     }
    
     While 
     {
     }
    
     for (int i = 0; i<=10;i+2)
     {
     }
    

        5
  •  1
  •   Community Mohan Dere    9 年前

    Is Nothing

    If

    ocdecio Set

    推荐文章