In my visual basic .net app I want to do comparison like
If myObject IsNot Nothing And myObject.property <> 0 Then
...
EndIf
The problem is that I get null reference exception when myObject is nothing. So I have to nest IF statements:
If myObject IsNot Nothing Then
If myObject.property <> 0 Then
...
EndIf
EndIf
Is it possible to do this comparison without nesting IFs?
AndAlso is a shortcircuited 'And'. If the first part isn't evaluated to true then the second part won't be evaluated and hence won't error
you can try like this:
If Not IsNothing(myObject) AndAlso myObject.property <> 0 Then
...
EndIf
Maybe it work for you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.