VB Quark #3: operator differences in VB/C#

Can you spot the problem with this code:

   <Extension()>
  
Public Function ToColor(argb As UInteger) As Color
      Return Color
.FromArgb( _
                     
CType((argb & &HFF000000) >> &H18, Byte
), _
                     
CType((argb & &HFF0000) >> &H10, Byte
), _
                     
CType((argb & &HFF00) >> 8, Byte
), _
                     
CType(argb & &HFF, Byte
))
  
End 
Function

 

If you said it should be the bitwise And operator not the string concatenation operator give yourself a pat on the back.  The problem here is three fold:

  1. The code is a failed attempt at translating from C#, and
  2. The project must have Option Strict Off for this code to even compile.
  3. No testing or unit tests to check if the code  works at runtime.

Sadly this is all too common. The above is in fact a very real example taken from the Silverlight Toolkit samples on Codeplex .

So the steps to correct this are first be aware of the language equivalents when translating from C# to VB. See the MSDN documentation on language equivalents for a great starting point. And take note of the operator equivalents. Some of the most common operators where people seem to make mistakes are:

operator VB C#
bitwise And And &
bitwise Or Or |
bitwise exclusive Or XOr ^
Short circuited Boolean And AndAlso &&
Short circuited Boolean Or OrElse ||
equality = ==
assignment = =
not equal <> !=
exponent ^  
modulus Mod %
string concatenation & +

From the short list above you can see the ^ and & operators have very different meanings in VB compared to C#. Which leads us to items 2 & 3.

If you are unsure of VB, or are translating and hence have a lot of code that *may* be invalid you can improve catching of these kind of developer mistakes by turning Option Strict On. In the original example, the argb & &HFF000000 would have given the result of a string: argb.ToString followed by –16777216. At runtime this string would then try to be converted to a number and then shifted right, most likely failing.

So if you turn Option Strict On, you’d immediately get warnings on trying to convert the string to a long. But even once you’ve got it compiling with Strict On, you should test the code. A lot of code you can quickly query in the immediate window, or write unit tests.

Oh, and the corrected code in this particular case is:


   <Extension()>
  
Public Function ToColor(argb As UInteger) As Color
      Return Color
.FromArgb( _
                     
CType((argb And &HFF000000) >> 24, Byte
), _
                     
CType((argb And &HFF0000) >> 16, Byte
), _
                     
CType((argb And &HFF00) >> 8, Byte
), _
                     
CType(argb And &HFF, Byte
))
  
End 
Function

 

For a complete project that fixes the VB samples for the Silverlight Toolkit, see my blog post from last month

This entry was posted in 13569, 15036, 16311, 491, 5162, 5726, 6184, 7187, 7341. Bookmark the permalink.

One Response to VB Quark #3: operator differences in VB/C#

  1. Zloth says:

    Well, actually & and + are both string concatenation in VB. + will only work between two strings/chars while the & can handle numbers.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>