【问题标题】:Check if TcpClient was not called -X IsNot Nothing vs Not X Is Nothing [vb.net]检查是否没有调用 TcpClient -X IsNot Nothing vs Not X Is Nothing [vb.net]
【发布时间】:2018-08-06 04:53:57
【问题描述】:

如何检查该函数之前是否定义了 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

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

【问题讨论】:

    标签: vb.net tcp tcpclient


    【解决方案1】:

    IsNot。一个字:

    If clientTest IsNot Nothing ...
    

    或者,你可以这样写:

    If Not (clientTest Is Nothing) ...
    

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

    相反,您希望Is 运算符的IsNot 逻辑逆运算,或者只是将逻辑Not 放置在Is 运算符以否定整个布尔表达式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 2019-09-21
      • 2020-06-14
      • 2010-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多