Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a VB.NET expression that *always* yields null?

Tags:

c#

.net

vb.net

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):

  • Declare an Object variable or field, set it to Nothing and use that for ...,
  • define a method or property that always returns Nothing,
  • DirectCast the second parameter (5) to Object.

Note: The example above is just an example. The question itself is written in bold.

like image 978
Heinzi Avatar asked Mar 03 '14 13:03

Heinzi


2 Answers

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...

like image 138
Jon Skeet Avatar answered Oct 02 '22 05:10

Jon Skeet


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.

like image 33
Steven Doggart Avatar answered Oct 02 '22 05:10

Steven Doggart