1

In C# we have this conditional assignment:

var test = 1;
var something = (test == 1)? "P":"C";

Or

var test = null;
var something = test ?? "";

Is it possible to do it in vb.net?

I am using to program in c# but in this project I am programming in vb.net, and I don't remember if it is possible to do it.

Dave
  • 7,028
  • 11
  • 35
  • 58
  • One duplicate: http://stackoverflow.com/questions/576431/is-there-a-conditional-ternary-operator-in-vb-net – J. Steen Dec 02 '14 at 12:20
  • Another duplicate: http://stackoverflow.com/questions/6792729/vb-net-null-coalescing-operator – Mephy Dec 02 '14 at 12:22
  • The other question is answered by using the version of `if` that takes only two parameters – J. Steen Dec 02 '14 at 12:22
  • possible duplicate of [Is there a VB.NET equivalent for C#'s ?? operator?](http://stackoverflow.com/questions/403445/is-there-a-vb-net-equivalent-for-cs-operator) – Abbas Dec 02 '14 at 12:30
  • when I was finding the question I didnt find them – Dave Dec 02 '14 at 14:28

1 Answers1

2

It is the If-operator which can be used with one or two parameters. The null-coalescing operator(??) in C# is the If with one parameter and the conditional operator(?) is the one with two parameters.

"Conditional-Operator"

Dim test As Int32 = 1
Dim something As String = If(test = 1, "P", "C")

"Null-Coalescing-Operator"

Dim test As String = Nothing
Dim something As String = If(test, "") ' "" is the replacement value for null '

Note that the If-operator is not the same as the old IIf-function. : Performance difference between IIf() and If

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939