80

Possible Duplicates:
Coalesce operator and Conditional operator in VB.NET
Is there a VB.NET equivalent for C#'s ?? operator?

Is there a built-in VB.NET equivalent to the C# null coalescing operator?

Community
  • 1
  • 1
RiddlerDev
  • 7,370
  • 5
  • 46
  • 62
  • 1
    possibel dup of http://stackoverflow.com/questions/629036/coalesce-operator-and-conditional-operator-in-vb-net – Mrchief Jul 22 '11 at 16:03
  • 1
    Since VS 2015, its now possible to use ?. in vb.Net. `Dim x = Obj?.Child?.AnotherChild?.Something?.AString` x is a string that will be Nothing if any object is nothing, or set if all objects are not nothing. – Edamreed Sep 11 '18 at 13:25
  • @Edamreed you shoud make that an answer on at least one of the duplicates or this question. – beppe9000 Sep 09 '19 at 18:20
  • @Edamreed `?.` is the null-conditional operator (aka safe navigation operator), not the `??` null-coalescing operator that the question asks about. – rob3c Nov 20 '20 at 04:26

4 Answers4

123

Yes, there is, a long as you're using VB 9 or later (included with Visual Studio 2008).

You can use the version of the If operator overloaded to accept only two arguments:

Dim myVar? As Integer = Nothing
Console.WriteLine(If(myVar, 7))

More information can be found here in a blog post by the VB.NET team.

(Yes, this is an operator, even though it looks like a function. It will compile down to the same IL as the "proper" null-coalescing operator 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.
JohnLBevan
  • 22,735
  • 13
  • 96
  • 178
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 1
    It should be noted that as opposed to the C# operator, the VB.NET implementation does not allow for throw expressions. It only accepts value expressions. – Crono Jul 08 '21 at 12:48
  • Linked blog article on the Wayback Machine: https://web.archive.org/web/20150716145313/http://blogs.msdn.com/b/vbteam/archive/2008/03/11/if-operator-a-new-and-improved-iif-sophia-salim.aspx – amonroejj Apr 05 '22 at 13:53
11

According to this question it would seem the answer is If()

Community
  • 1
  • 1
SeeSharp
  • 2,760
  • 16
  • 15
  • 4
    If you find a duplicate question, please flag the question as a duplicate rather than posting an answer that links to the duplicate question. That helps to keep down clutter on the site. Only post an answer if you think you can *add value* to the question. Thanks! – Cody Gray - on strike Jul 22 '11 at 16:08
  • Sorry, my bad. Didn't realise; will certainly bear it in mind for the future though. – SeeSharp Jul 22 '11 at 16:09
-3

No. Use GetValueOrDefault; that's why it's there!

user541686
  • 205,094
  • 128
  • 528
  • 886
  • 2
    `GetValueOrDefault(ByVal default As T)` forces evaluation of the default parameter even if it is not required, the null-coalescing operator will only evaluate default if `HasValue` is false. – Lukazoid Jul 03 '15 at 16:13
-3

I don't believe that there is a built in VB.Net equivalent, but here's an answer: null coalesce operator in VB.Net(8)

Community
  • 1
  • 1
davecoulter
  • 1,806
  • 13
  • 15
  • 6
    If you find a duplicate question, please flag the question as a duplicate rather than posting an answer that links to the duplicate question. That helps to keep down clutter on the site. Only post an answer if you think you can *add value* to the question. Thanks! – Cody Gray - on strike Jul 22 '11 at 16:08
  • @Cody Gray -- thanks, will do next time. – davecoulter Jul 22 '11 at 17:11