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#?
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#?
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)
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.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.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
Coalesce() implementation around.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)If should be IIf
Dim x as Integer=IIf(a,b,c)
Dim x = Obj?.Child?.AnotherChild?.Something?.AStringx is a string that will be Nothing if any object is nothing, or set if all objects are not nothing.