If "Not X Is Nothing Then" replacement in VB.Net [duplicate] - vb.net

This question already has answers here:
"Not ... Is Nothing" versus "... IsNot Nothing"
(5 answers)
Closed 5 years ago.
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?

Use
If X IsNot Nothing Then
reference: https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/isnot-operator

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)

Related

Why in the conditionnal "if" assignement do not assign nothing/null value to a variable? [duplicate]

This question already has answers here:
Ternary operator VB vs C#: why resolves Nothing to zero? [duplicate]
(7 answers)
Closed 3 years ago.
I was surprised when I tried to assign null value to a variable using ternary expression in vb.net. When I assign through ternary expression, it doesn't works as expected.
Dim i As Integer? = Nothing
Dim j As Integer? = Nothing
i = If(True, j, 1)
j = If(True, Nothing, 1)
After execution of this code: i is nothing but j becomes 0 (zero). Why?
What is the explanation?
Why I can"t assign directly Nothing (Null) value?
I think the important thing to understand here is Nothing in VB.Net is not the same as null in other languages, because you can still assign Nothing to value types. In many other languages, null is a reference-type construct only. If you're familiar with C#, Nothing is closer to default(T) than to null.
With that in mind, take a fresh look at this expression:
If(True, Nothing, 1)
The compiler evaluates the entire expression on it's own merits, knowing nothing about i or j, even though j is the target of the assignment. The expression has to be able to stand alone.
VB.Net must also determine a type to use for the expression, and it must do this at compile-time. It is not able to infer anything from the use of Nothing about needing an Integer? rather than a basic non-nullable Integer for this type, because VB.Net is perfectly happy to assign Nothing to value types. Therefore the type of the conditional expression can only be inferred from the 1 literal in the final argument, which is a plain Integer, and not Integer?.
Given that resulting type, we now must evaluate Nothing as an integer, where the result is the 0 you observed. In theory, this part is done at runtime rather than compile time, but in practice I suspect the compiler or jitter recognizes the chance to optimize things and rewrites it all down to just j = 0.
If you want to be able to assign an Integer? with a value of Nothing, do it as you did in the first example and keep a variable handy with the correct type you can use for the assignment.

IF Without End IF in VBA [duplicate]

This question already has answers here:
IF statements in VBA
(5 answers)
Closed 7 years ago.
see the code below.
IF Weight=0 Then CallFunction
IF Weight=100 Then
Heavy=True
Else
Heavy= False
End If
How is the program working when I have not ended a if statement?
its VBA's functionality to end up if statements in a single line..
If Weight is equal to zero, then CallFunction is executed (if Weight is not equal to zero, a CallFunction is not executed), then the code is going to another if statement. That's all! Debug the program to find out ;)

"Not ... Is Nothing" versus "... IsNot Nothing"

Does anyone here use VB.NET and have a strong preference for or against using Not foo Is Nothing as opposed to foo IsNot Nothing? If so, why?
For Example
If var1 IsNot Nothing Then
...
End If
and
If Not var1 Is Nothing Then
...
End If
I just want to know which one is better?
Are they both equally acceptable?
The
If Not var1 Is Nothing Then
Is a hangover from VB6. There didn't used to be an IsNot, and so this was the only way to determine if a variable was not Nothing. It seems to be redundant in VB.NET.
foo IsNot Nothing
The following line is straight from Microsoft's Visual Basic Coding Conventions:
Use the IsNot keyword instead of Not...Is Nothing.
I would go with the first variant - it reads like English and is easier to follow/understand than the second one. Other than that, they are equivalent.
I found a similar question here VB.NET - IsNothing versus Is Nothing, where I feel this question was exhaustively answered. Among the answers Jack Snipes identified http://weblogs.asp.net/psteele/410336, a blog that gives some extra detail. From those I prefer and have used
IsNot Nothing
which also makes my code easier to read and understand.
Using VB 7.0
If var1 Is Not Nothing Then
generates an "invalid use of object error" as per this "VBForums" link.
If var1 IsNot Nothing Then
generates a "Compile error: Expected: Then or GoTo"
If Not IsNothing(var1) Then
worked like a champ

VB.NET logic order in if statement

I'm new in VB.NET, but for C, C++, C# and other languages I had some years of expericences. This problem for me is very weird because I never met it before.
I have this line of code:
If obj is Nothing Or obj.IsDisposed Then
'do some stuffs
End If
This line of code will reveal an error when obj is Nothing because obj.IsDisposed doesn't exist (no handle for it). As what I know, the first statement of Or it returns True so the result of If statement in anycase would be True.
Can anyone give me an instruction how to get rid of this (or I have to write If..Then..Else If..End If)
try OrElse, the obj.Disposed will not be evaluated when "obj is Nothing" is true
If obj is Nothing OrElse obj.IsDisposed Then
'do some stuffs
End If
You can use the OrElse Operator it will bypass the second evaluation if the first is true.
From above Link:
A logical operation is said to be short-circuiting if the compiled code can bypass the evaluation of one expression depending on the result of another expression. If the result of the first expression evaluated determines the final result of the operation, there is no need to evaluate the second expression, because it cannot change the final result. Short-circuiting can improve performance if the bypassed expression is complex, or if it involves procedure calls.
OrElse is what you need. it will only evaluate on the first evaluation as long as it is already true
If obj is Nothing OrElse obj.IsDisposed Then
'do some stuffs
End If

What is VB.NET's equivalent of C#'s default keyword? [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Bug?? If you assign a value to a nullable integer via a ternary operator, it can't become null
While this question may seem like a duplicate of many, it is actually being asked for a specific reason. Take this code, for example:
Dim n As Integer? = If(True, Nothing, 1)
In that code, the ternary expression should be returning Nothing, but it's setting n to 0. If this were C#, I could say default(int?) and it would work perfectly. Now it looks like I am going to have to ditch the ternary and use a regular If block, but I really want to use the ternary.
If Nothing were truly VB.NET's equivalent to C#'s default, how can you explain this behavior?
The VB.NET equivalent to C#'s default is the keyword Nothing. The code you wrote should compile just fine so long as Id.Value returns an Integer value.
The reason your updated sample is going wrong is because of the nature of Nothing. In VB.NET Nothing is the empty value and it's convertible to any type. Now for an If expression, the compiler has to infer what the type of the return should be, and it does this by looking at the two value arguments.
The value Nothing has no type, but the literal 1 has the type Integer. Nothing is convertible to Integer so the compiler determines Integer is the best type here. This means when Nothing is chosen as the value, it will be interpreted as Integer and not Integer?.
The easiest way to fix this is to explicitly tell the compiler that you want 1 to be treated as an Integer?.
Dim n As Integer? = If(True, Nothing, CType(1, Integer?))