Microsoft Most Valuable Professional

Chris Pietschmann

An MVP From Wisconsin



A Couple VB.NET Language Tips for C# Developers

Originally, I started out as a Visual Basic developer, and have since moved mostly to C#. However, when doing consulting work, I do need to cross back and forth quite often. Here are a couple VB.NET tips that you probably aren't aware of if you're mostly a C# developer. Some VB.NET developers may not even know about then either.

Null Coalesce

Null Coalescing is really simple in C#:

// If "someValue" is Null then set "i" to 0 (zero)
// otherwise set it to the value of "someValue"
int? i = someValue ?? 0;

But what about VB.NET?

<code:VB>

Dim i = If(someValue, 0)

</code>

Granted the VB.NET Null Coalesce is a method call, but at least there's still an equivalent available in the language. Also, I believe this is something that was introduced in VB.NET 9.0.

Ternary Conditional Operator

This is the ability have a complete If..Else..Then statement on a single line and have it return a value. This is really simple in C#:

// This performs the same logical operation as the Null Coalesce example above
int? i = (someValue == null ? 0 : someValue);

How about in VB.NET?

<code:VB>

Dim i = If(someValue = Nothing, 0, someValue)

'' The above can be simplified, since if the first parameter is equal to "Nothing"
'' then the "true" (second) parameter is return, otherwise the
'' "false" (third) parameter is returned.
Dim i = If(someValue, 0, someValue)

'' Also to further simplify, you can just pass in the "false" (second) parameter
'' and if its equal to "Nothing" then the "false" (second) parameter is returns,
'' otherwise the value itself is returned.
Dim i = If(someValue, 0) 

</code>

Lock Statement

You may be familiar with the lock statement in C#, especially if you're used to worrying about concurrency.

lock (expression)
{

    ...Some Code...

}

At first it appears to not exist in VB.NET, but they just named it SyncLock instead:

<code:VB>

SyncLock (expression)

    ...Some Code...

End SyncLock

</code>

 

Please, excuse the bad syntax highlighting for the VB.NET code, it seems that my instance of BlogEngine.NET doesn't like to highlight too many blocks of code within the same post.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: General
Posted by crpietschmann on Friday, October 31, 2008 12:27 AM
Permalink | Comments (1) | Post RSSRSS comment feed

Related posts

Comments

Andre P. us

Saturday, December 20, 2008 1:24 PM

Andre P.

I thought ternary expressions were possible in the past using the IIf statement... the problem has to do with when the expressions are evaluated. IIRC, IIf(..) expressions are evaluated prior to selection of the true/false functionality, which means that you can't do things like MyObjectProperty = CType(IIf(MyObject Is Nothing, DefaultPropertyValue, MyObject.Property), MyPropertyType) because the MyString.Property expression is evaluated prior to determining whether "MyObject Is Nothing" is true or false. Just checking the VB9 documentation:

"Unlike the IIF runtime function, however, a conditional expression only evaluates its operands if necessary."

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

Tuesday, January 06, 2009 4:52 AM

About the author

I'm Chris Pietschmann, go to the About Me page to learn more about me.

Search

Sponsors

Web.Maps.VE - ASP.NET AJAX Virtual Earth Mapping Server Control

Recent comments

Disclaimer


This work is licensed under a Creative Commons Attribution 3.0 United States License, unless explicitly stated otherwise within the posted content.
© Copyright 2004 - 2009 Chris Pietschmann