Is there a VB.NET equivalent for C#'s '??' operator?

Solution 1:

Use the If() operator with two arguments (Microsoft documentation):

' Variable first is a nullable type.
Dim first? As Integer = 3
Dim second As Integer = 6

' Variable first <> Nothing, so its value, 3, is returned.
Console.WriteLine(If(first, second))

second = Nothing
' Variable first <> Nothing, so the value of first is returned again. 
Console.WriteLine(If(first, second))

first = Nothing second = 6
' Variable first = Nothing, so 6 is returned.
Console.WriteLine(If(first, second))

Solution 2:

The IF() operator should do the trick for you:

value = If(nullable, defaultValueIfNull)

http://visualstudiomagazine.com/listings/list.aspx?id=252

Solution 3:

The accepted answer doesn't have any explanation whatsoever and is simply just a link.
Therefore, I thought I'd leave an answer that explains how the If operator works taken from MSDN:


If Operator (Visual Basic)

Uses short-circuit evaluation to conditionally return one of two values. The If operator can be called with three arguments or with two arguments.

If( [argument1,] argument2, argument3 )


If Operator Called with Two Arguments

The first argument to If can be omitted. This enables the operator to be called by using only two arguments. The following list applies only when the If operator is called with two arguments.


Parts

Term         Definition
----         ----------

argument2    Required. Object. Must be a reference or nullable type. 
             Evaluated and returned when it evaluates to anything 
             other than Nothing.

argument3    Required. Object.
             Evaluated and returned if argument2 evaluates to Nothing.


When the Boolean argument is omitted, the first argument must be a reference or nullable type. If the first argument evaluates to Nothing, the value of the second argument is returned. In all other cases, the value of the first argument is returned. The following example illustrates how this evaluation works.


VB

' Variable first is a nullable type. 
Dim first? As Integer = 3
Dim second As Integer = 6

' Variable first <> Nothing, so its value, 3, is returned.
Console.WriteLine(If(first, second))

second = Nothing 
' Variable first <> Nothing, so the value of first is returned again.
Console.WriteLine(If(first, second))

first = Nothing
second = 6
' Variable first = Nothing, so 6 is returned.
Console.WriteLine(If(first, second))

An example of how to handle more than two values (nested ifs):

Dim first? As Integer = Nothing
Dim second? As Integer = Nothing
Dim third? As Integer = 6
' The LAST parameter doesn't have to be nullable.
'Alternative: Dim third As Integer = 6

' Writes "6", because the first two values are "Nothing".
Console.WriteLine(If(first, If(second, third)))

Solution 4:

You can use an extension method. This one works like SQL COALESCE and is probably overkill for what you are trying to test, but it works.

    ''' <summary>
    ''' Returns the first non-null T based on a collection of the root object and the args.
    ''' </summary>
    ''' <param name="obj"></param>
    ''' <param name="args"></param>
    ''' <returns></returns>
    ''' <remarks>Usage
    ''' Dim val as String = "MyVal"
    ''' Dim result as String = val.Coalesce(String.Empty)
    ''' *** returns "MyVal"
    '''
    ''' val = Nothing
    ''' result = val.Coalesce(String.Empty, "MyVal", "YourVal")
    ''' *** returns String.Empty
    '''
    ''' </remarks>
    <System.Runtime.CompilerServices.Extension()> _
    Public Function Coalesce(Of T)(ByVal obj As T, ByVal ParamArray args() As T) As T

        If obj IsNot Nothing Then
            Return obj
        End If

        Dim arg As T
        For Each arg In args
            If arg IsNot Nothing Then
                Return arg
            End If
        Next

        Return Nothing

    End Function

The built-in If(nullable, secondChoice) can only handle two nullable choices. Here, one can Coalesce as many parameters as desired. The first non-null one will be returned, and the rest of the parameters are not evaluated after that (short circuited, like AndAlso/&& and OrElse/|| )

Solution 5:

The one significant limitation of most of these solutions is that they won't short-circuit. They are therefore not actually equivalent to ??.

The built-in If operator won't evaluate subsequent parameters unless the earlier parameter evaluates to nothing.

The following statements are equivalent:

C#

var value = expression1 ?? expression2 ?? expression3 ?? expression4;

VB

dim value = if(expression1,if(expression2,if(expression3,expression4)))

This will work in all cases where ?? works. Any of the other solutions would have to be used with extreme caution, as they could easily introduce run-time bugs.