We all know that VB's Nothing
is similar, but not equivalent, to C#'s null
. (If you are not aware of that, have a look at this answer first.)
Just out of curiosity, I'd like to know the following:
Is there a VB.NET expression that always yields null
?
To give a concrete example, take the following statement:
Dim o As Object = If(myBool, 5, ...)
Is it possible to replace ...
with something, such that o
is 5
when myBool
is true and Nothing/null
when myBool
is false?
Obvious solutions that won't work:
Nothing
(see the question to the linked answer above),DirectCast(Nothing, Object)
(throws a compile-time error with Option Strict On),DirectCast(Nothing, Integer?)
works for this example, but does not work in general (if you replace 5
with 5.0
in this example, you'd need to modify the cast).Obvious workarounds (won't count as answers):
Object
variable or field, set it to Nothing
and use that for ...
,Nothing
,DirectCast
the second parameter (5
) to Object
.Note: The example above is just an example. The question itself is written in bold.
The first answer I gave missed some points, but this should do it:
Dim o As Object = If(myBool, 5, DirectCast(Nullable.GetUnderlyingType(GetType(Integer)), Object))
This uses the fact that Nullable.GetUnderlyingType
will return a null reference if you pass it a type which isn't a nullable value type - which Integer
isn't. Other alternatives exist, such as Type.GetElementType()
, or perhaps GetType(Object).BaseType
.
I've checked that this works with multiple different types for the second operand.
It's slightly annoying that you have to cast to Object
... I'm still working on alternatives for that...
The simple answer is, no. There is no expression in VB.NET that only returns null
. As you know, when the compiler parses a command using ternary operator, it infers the output type based on the two inputs. If one of the two inputs is Nothing
, it must rely solely on the other parameter. Therefore, the "right" way to do it in VB.NET is to first cast the other parameter to Object
, thereby forcing the output of the operation to be an Object
:
Dim o As Object = If(myBool, DirectCast(5, Object), Nothing)
If, however, you really need an in-line expression which, itself, always evaluates to null
, you could always do it by invoking a lambda expression, like this:
Dim o As Object = If(myBool, 5.0, (Function() Nothing).Invoke())
That syntax should work in any situation and would always result in Nothing
rather than potentially resulting in default value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With