Java ?: Operator in vb.net

Is there an equivalent operator ?: In .net? for example, in java I can do:

 retParts[0] = (emailParts.length > 0) ? emailParts[0] : ""; 

but not

 if (emailParts.length > 0) { retParts[0] = emailParts[0]; } else { retParts[0] = ""; } 

I would like to be able to do this in VB.NET.

+7
java conditional-operator language-features
source share
1 answer

Use if statement :

 ' data type infered from ifTrue and ifFalse ... = If(condition, ifTrue, ifFalse) 

This operator was introduced in VB.NET 9 (released with the .net Framework 3.5). In earlier versions, you would have to resort to the IIf function (no type inference, no short circuit):

 ' always returns Object, always evaluates both ifTrue and ifFalse ... = IIf(condition, ifTrue, ifFalse) 
+9
source share

All Articles