Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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.

System.DBNull.Value is most likely what you're after.

Expansion, from Tech Republic:

... The following C# code determines if a string value is null:

string sTest = "Test"; if (sTest == null) { Console.WriteLine("sTest is Null"); }

This code works without any problems with C#, but there's no VB.NET equivalent of the null keyword. Instead, VB.NET uses the Nothing keyword. The following code demonstrates its use:

Dim sTest As String
If (sTest Is Nothing) Then
    Console.WriteLine("sTest is Null") 
End If

Another area of confusion stems from the fact that VB.NET doesn't treat null and Nothing as equals; consequently, a VB.NET programmer may have to check for both values.

The variation in support may cause confusion, but a developer rarely develops in both languages simultaneously. On the other hand, Microsoft provides a uniform method for working with null values: The base System.Convert namespace includes the DBNull object.

DBNull

The use of the DBNull class indicates the absence of a known value. While the Microsoft documentation says it's typically in a database application, you may also use it with any type of data.

If (sTest.Equals(System.DBNull.Value) Then
    Console.WriteLine("sTest is Null")
End  If 

-- verbatim from Working with Null values in the .NET Framework

You can create one yourself (pick a different name for the function if you like):

Private Function ValueOrNull(expression As Boolean, value As Object) As Object
  If expression Then Return value
  Return Nothing
End Function

Usage:

Dim myBool As Boolean
Dim o As Object = ValueOrNull(myBool, 5)

Works with any type, including your example with 5.0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top