检查是否未调用 TcpClient -X IsNot Nothing vs Not X Is Nothing [vb.net]

Check if TcpClient was not called -X IsNot Nothing vs Not X Is Nothing [vb.net]

如何查看函数之前是否定义过TcpClient? 我正在使用一个 Sub 打开一个连接,并使用一个计时器来检查连接是否仍然存在(使用相同的 Sub!)。

Public clientTest as TcpClient = nothing

Sub OpenCheckConnection()
  If clientTest is not Nothing AndAlso clientTest.Connected = True Then
     pictureboxTest.Image = xxGreen
  Else
     clientTest = New TcpClient
     Try
       clientTest.Connect(IPtest, Porttest)
     Catch ex As Exception
       NewLog(ex.ToString)
     End Try
'
      If clientTest.Connected = True Then
      (...)                 '
      Else
         pictureboxTest.Image = xxRed
      End If
  End If
End Sub

我已经测试过了,clientTest is not Nothing 没有为 TcpClient 定义。 "clientTest <> 0 也没有定义。 我该如何管理?

IsNot。一个字:

If clientTest IsNot Nothing ...

或者,您可以这样写:

If Not (clientTest Is Nothing) ...
引用类型的

Nothing 是一个特殊值,通过逻辑 Not 直接否定它对您没有帮助。未知值的逻辑 Not 仍然是未知值。

相反,您想要 Is 运算符的 IsNot 逻辑逆运算,或者只需要 Is 运算符,您将逻辑 Not 放在其中以否定整个运算符布尔表达式。