Nathan Koop Nathan Koop - 2 months ago 5
Vb.net Question

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

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

??
operator?

Answer

the If() staetement. From MSDN:

If( [argument1,] argument2, argument3 )

When If is called by using three arguments, the first argument must evaluate to a value that can be cast as a Boolean. That Boolean value will determine which of the other two arguments is evaluated and returned. The following list applies only when the If operator is called by using three arguments.

...

 This statement prints TruePart, because the first argument is true.
Console.WriteLine(If(True, "TruePart", "FalsePart"))

' This statement prints FalsePart, because the first argument is false.
Console.WriteLine(If(False, "TruePart", "FalsePart"))

Dim number = 3
' With number set to 3, this statement prints Positive.
Console.WriteLine(If(number >= 0, "Positive", "Negative"))

number = -1
' With number set to -1, this statement prints Negative.
Console.WriteLine(If(number >= 0, "Positive", "Negative"))