When we want to check not null in vb.net we use "IF Not X Is Nothing Then" but this is very vague to read and understand especially if code is complicated or large. Is there any condition with which we can check the same condition and easy to read or understand?
Asked
Active
Viewed 1,517 times
0
-
The `IsNot` operator has been available in VB for quite a while now, so you should be using `If X IsNot Nothing Then`. – jmcilhinney Aug 10 '17 at 13:45
2 Answers
3
Use
If X IsNot Nothing Then
reference: https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/isnot-operator

Gabriele Petrioli
- 191,379
- 34
- 261
- 317
0
There are multiple ways to do this.
As suggested by other comments/answers you can use the IsNot
operator.
Or you can use the IsNothing()
function. E.g.
If Not IsNothing(someObject) Then ...
You can also use the Iif()
function but I don't recommend it since it don't do short-circuiting and always evaluates all the arguments, regardless of the condition.
Probably the inline if is what you're looking for (available since VB.NET 2008 onward):
If(condition, truePart, falsePart)

Bozhidar Stoyneff
- 3,576
- 1
- 18
- 28