Yes, there is, as long as you use VB 9 or later (included with Visual Studio 2008).
You can use the If version of the operator to accept only two arguments:
Dim myVar? As Integer = Nothing Console.WriteLine(If(myVar, 7))
More information can be found here in the blog post by the VB.NET team.
(Yes, this is the operator although it looks like a function. It will be compiled to the same IL as the βcorrectβ operator with zero coalescing in C #.)
Example
Dim b As Boolean? Console.WriteLine("{0}.", If(b, "this is expected when b is nothing")) 'output: this is expected when b is nothing. b = False Console.WriteLine("{0}.", If(b, "this is unexpected when b is false")) 'output: False. b = True Console.WriteLine("{0}.", If(b, "this is unexpected when b is true")) 'output: True.
Cody Gray Jul 22 2018-11-11T00: 00Z
source share