22

Possible Duplicate:
Is there a conditional ternary operator in VB.NET?

Can we use the Coalesce operator(??) and conditional ternary operator(:) in VB.NET as in C#?

peterh
  • 11,875
  • 18
  • 85
  • 108
user42348
  • 4,181
  • 28
  • 69
  • 98
  • 10
    This is not a duplicate question. This question involves the null-coalescing operator. – Andrew Hoffman Nov 18 '14 at 21:12
  • 2
    For converting ?? to VBnet use If(,) with two parameters as mentioned [here](http://stackoverflow.com/a/6792791/521554) – LosManos Mar 21 '15 at 20:40
  • If it is not .duplicate it is to broad because it includes two questions – IvanH Apr 30 '18 at 18:36
  • 3
    Since VS 2015, its now possible to use ?. in vb.Net. `Dim x = Obj?.Child?.AnotherChild?.Something?.AString` x is a string that will be Nothing if any object is nothing, or set if all objects are not nothing. – Edamreed Sep 11 '18 at 13:21

4 Answers4

21

I think you can get close with using an inline if statement:

//C#
int x = a ? b : c;

'VB.Net
Dim x as Integer = If(a, b, c)
Nick Josevski
  • 4,156
  • 3
  • 43
  • 63
  • 3
    *Note: using the if statement that way only applies in VB.NET 2008 and onwards. – Nick Josevski Oct 23 '10 at 11:30
  • 2
    To use the If() function as a coalesce operator, it must be called with just two parameters, and it must be used for reference types: `Dim objC = If(objA,objB)` This would set objC to objA unless objA is Nothing, in which case objC would be set to objB, whether it is Nothing or not. – Guillermo Prandi Oct 05 '15 at 17:27
  • For 2006+ support use IIf from memory? – brandito Apr 06 '18 at 05:48
  • 1
    @Brandito: the `IIF` function is just that, a function, one that you could write yourself. It does not do coalescence, you would have to write that function yourself. `If` is a builtin operator, and does do coalescence if you only provide 2 arguments. – jmoreno Jul 26 '18 at 10:57
  • 1
    Given that IIf( ) is a function and If( ) is not, the main side effect is that If( ) only evaluates the true/false side, where IIf( ) will evaluate BOTH sides, even if it only returns one of them. – Brain2000 Oct 31 '19 at 17:17
13
Sub Main()
    Dim x, z As Object
    Dim y As Nullable(Of Integer)
    z = "1243"

    Dim c As Object = Coalesce(x, y, z)
End Sub

Private Function Coalesce(ByVal ParamArray x As Object())
    Return x.First(Function(y) Not IsNothing(y))
End Function
Mina Luke
  • 2,056
  • 1
  • 20
  • 22
  • Utilizing LINQ, this is the most effective `Coalesce()` implementation around. – miroxlav Feb 17 '14 at 18:24
  • 6
    The problem with this (and ivan's below), is that all parameters will be evaluated. So, if I write `Dim thingie = Coalesce(Session("thingie"), new Thingie)` a new Thingie object will be created every time (although it will be thrown away if a Thingie exist in the Session) – James Curran Oct 23 '15 at 23:20
6

just for reference, Coalesce operator for String

Private Function Coalesce(ByVal ParamArray Parameters As String()) As String
    For Each Parameter As String In Parameters
        If Not Parameter Is Nothing Then
            Return Parameter
        End If
    Next
    Return Nothing
End Function
ivan
  • 61
  • 1
  • 1
-3

If should be IIf

Dim x as Integer=IIf(a,b,c)

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • 7
    Nope. IIf evaluates all parameters since it is a regular call. See http://dotnetslackers.com/VB_NET/re-55021_IIF_becomes_If_and_a_true_ternary_operator.aspx – LosManos Dec 18 '12 at 16:08