2

What is the VB.NET equivalent of the C# ? operator?

For example, how would the following code be written in VB.NET?

hp.pt = iniFile.GetValue("System", "PT").ToUpper().Equals("H") ? PT.PA : PT.SP
CJ7
  • 21,593
  • 63
  • 183
  • 312

4 Answers4

10

Historically, IIf was commonly used for that - but that does not use short-circuiting so is not quite the same. However, there is now a 3-part If:

hp.pt = If(iniFile.GetValue("System", "PT").ToUpper().Equals("H"), PT.PA, PT.SP)

that does use short-circuiting, and thus is identical to the conditional operator in C#.

Marc Gravell
  • 976,458
  • 251
  • 2,474
  • 2,830
2

You can use the If operator

hp.pt = If(iniFile.GetValue("System", "PT").ToUpper().Equals("H"), PT.PA, PT.SP)
keyboardP
  • 67,723
  • 13
  • 149
  • 201
2

Try using the If function like so:

x = If(condition, trueValue, falseValue)
Paul
  • 4,050
  • 3
  • 27
  • 51
2

This question is a duplicate of a question that has already been asked and answered:

Is there a conditional ternary operator in VB.NET?

here:

Dim foo as String = If(bar = buz, cat, dog)
Community
  • 1
  • 1
Mr. Mr.
  • 4,227
  • 3
  • 26
  • 41