VB.Net oddity of the day – Assignment/Comparison operator

On my current project I’m forced to code in VB.Net.  Normally I’m pretty open to other languages, but VB.Net is irritating (more so than VB6 in my opinion).  The language syntax is riddled with ambiguity (at least for a c-type guy like myself) and moving from c# to VB can be confusing.

Here’s my latest beef with VB.Net.  There’s only one operator for assignment and comparisons.  As a result, the compiler handles evaluation of the the language syntax in a subtle, but irritating way.

Here’s some code to illustrate my point, I had something like this in my program:

Dim oldValue, currentValue As Integer

oldValue = currentValue = 2

In C# this is cut and dry.  The value 2 would be assigned to currentValue and the side effect would be the value assigned, or 2.  I’ve long been in the habit of using assignment side effects in C#, but VB looks at this differently.  In VB, the value 2 is assigned to currentValue, but the side effect is the comparison of the two values.  In this case, currentValue initializes to 0, so the expression evaluates to false (or 0).  Therefore, oldValue now equals 0.

Now, if I did this:

Dim oldValue, currentValue As Integer

currentValue = 2

oldValue = currentValue = 2

When the statement is evaluated, it will first evaluate the expression currentValue = 2.  2 will be assigned to currentValue and the side effect will be the comparison of currentValue to the assigned value, 2.  They are equal, so true will be returned and converted from bool to int as the non-zero value -1.

It’s no biggy, just annoying.

About Joshua Lockwood

I code stuff and people pay me for it.
This entry was posted in Programming, VB.Net. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

6 Responses to VB.Net oddity of the day – Assignment/Comparison operator

  1. Jeff Smith says:

    Three words for you:


    If you ain’t programming in VB.NET with option strict enabled, then you ain’t programming!

  2. Jeff Smith says:

    Three words for you:


    If you ain’t programming in VB.NET with option strict enabled, then you ain’t programming!

  3. Jeff says:


    Problem solved!

  4. Chris Koenig says:

    I’ve not used VB.NET for a long time, although we did do a huge project with it for an insurance company in ohio a few years back. I’d forgotten about some of the nuance-type differences between VB.NET and C#…

    One thing that strikes me as interesting though is your use of the phrase “side effect”. For me, when I hear the words “side effect” I think of someone breaking out in hives because the medicine they took has that “side effect” to balding men in their late thirties with a *slight* weight problem. Should we be encouraging the active use of “side effects” in our software?

    I could argue that the approach:

    oldValue = 2
    currentValue = 2

    is much clearer to read, and understand to both the experienced and novice developer, works exactly the same way in most modern programming languages, and is generally preferrable than relying on “side effects” or “quirks” in a given language.

    I’m interested in your thoughts here – to the VB programmer that’s trying to learn C#, they might see this scenario as equally frustrating in terms of unexpected behavior going the other direction, and might not carry a bottle of Benedryl around with them like I do…

  5. jlockwood says:

    Spot on Jeff, I agree with all of you (I do use Option Explicit On and have started using Option Strict On too). The use of side effects is a bad habit (see? I admit it!) carried over from C. When I first worked with VB/COM (back in the day), I had the habit of using return codes in my functions (not to mention writing all my interfaces in MIDL). I can see how VB guys switching to C# could be turned off in exaclty the same way by abusing ternary operators, habitual bit twiddling, increment/decrement operators, sqeezing as much code as one can on a single line, etc. Over all I’m entertained by the differences between the two langauges (am only really irritated the ubiquitous parens and the need for line continuations…esp when needed for attributes).

  6. Jon says:

    Hey Jeff, I’ve starting coding in C++ years and years ago, but for the past 3 years have been doing mostly VB.Net. The other day I pulled out an old program I had and I was appalled!! The c++ syntax, like you say, encourages you to “sqeeze as much code as one can on a single line”. Add to that the fact that most of that line is going tovarious symbols, and you can very easily create lines whose meaning will be very difficult to grasp at first glance.

    That being said, yes my initial move to VB.Net was difficult (I mainly miss the for loops and arrays/pointers of C++), but eventually it grew on me as I was able to do most things a lot faster.

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>