24

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#?

4
  • 11
    This is not a duplicate question. This question involves the null-coalescing operator. Commented Nov 18, 2014 at 21:12
  • 3
    For converting ?? to VBnet use If(,) with two parameters as mentioned here Commented Mar 21, 2015 at 20:40
  • If it is not .duplicate it is to broad because it includes two questions Commented Apr 30, 2018 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. Commented Sep 11, 2018 at 13:21

4 Answers 4

22

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)
Sign up to request clarification or add additional context in comments.

5 Comments

*Note: using the if statement that way only applies in VB.NET 2008 and onwards.
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.
For 2006+ support use IIf from memory?
@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.
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.
12
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

2 Comments

Utilizing LINQ, this is the most effective Coalesce() implementation around.
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)
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

Comments

-4

If should be IIf

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

1 Comment

Nope. IIf evaluates all parameters since it is a regular call. See dotnetslackers.com/VB_NET/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.