VB.NET null coagulation operator?

Possible duplicates:
Coalesce statement and conditional statement in VB.NET
Is there a VB.NET equivalent for C #? operator?

Is there a built-in VB.NET equivalent for a zero-coalescing C # operator?

+44
null-coalescing-operator
Jul 22 '11 at 16:00
source share
4 answers

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. 
+67
Jul 22 2018-11-11T00:
source share
β€” -

According to this question, it seems that the answer is If ()

+7
Jul 22 '11 at 16:04
source share

No. Use GetValueOrDefault ; why is he there!

-one
Jul 22 '11 at 16:03
source share

I do not believe that there is a built-in equivalent of VB.Net, but here is the answer: the null coalesce operator in VB.Net (8)

-one
Jul 22 '11 at 16:05
source share



All Articles