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
  • 22,579
  • 65
  • 193
  • 321

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
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2

You can use the If operator

hp.pt = If(iniFile.GetValue("System", "PT").ToUpper().Equals("H"), PT.PA, PT.SP)
keyboardP
  • 68,824
  • 13
  • 156
  • 205
2

Try using the If function like so:

x = If(condition, trueValue, falseValue)
Paul
  • 4,160
  • 3
  • 30
  • 56
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,257
  • 3
  • 27
  • 42