programming language textbooksA favorite college professor, when asked why programming classes were taught predominantly in C++ instead of Visual Basic, would reply, “C++ is a language for adults.”  Though perhaps indelicately stated, the thought was not without merit.  Not only is C++ inarguably the far more powerful language, but Visual Basic lacks strong typing, and its constructs require extra keystrokes (“End Function”, “End While”, “End If”, etc., instead of a simple “}” and “Dim x As Integer” instead of “int x”).

In the .NET application world, C++, though still far the most powerful of the .NET languages, is limited in that it cannot produce a pure managed .NET application compatible with Microsoft’s Code Access Security model.  So for new .NET-based enterprise applications, C# and VB.Net are the two predominant languages.

As an interesting aside, according to TIOBE’s rankings of programming language popularity, C and C++ still outrank C# and Visual Basic by far.  These rankings are based on internet searches, so they aren’t going to exactly reflect reality, but are interesting nonetheless.  One trend that jumps out from their graph, for instance, is that Objective-C (the language used to write software for the iPhone) has had something of a meteoric rise in popularity in the last two years.

Many of the differences between C# and VB.Net amount to “syntactic sugar”, meaning that they have no actual impact on the code generated.  Modules built from any .NET language are fully reversible using the free .NET Reflector and so we can actually take a look at the code generated and explore some of the VB-specific constructs that go beyond syntactic sugar.

One of the unique constructs in VB is the With block.  This statement allows a programmer to access multiple methods or properties for a single object, which may be the result of a function call or complicated lookup.  For example, consider this code segment:

With ListView1.Items(5)
.BackColor = Color.AliceBlue
.Checked = False
.Text = “test”
End With

By using the With block instead of writing ListView1.Items(5) three times, we not only save ourselves some typing and make our code more easily maintained, but we also save some nominal amount of processing time because the program only needs to look up the item in question one time.  Using the .Net Reflector, we can see that the With block is eliminated by the compiler and a variable is declared in its place.

ListViewItem VB$t_ref$L0 = this.ListView1.Items[5];
VB$t_ref$L0.BackColor = Color.AliceBlue;
VB$t_ref$L0.Checked = false;
VB$t_ref$L0.Text = “test”;
VB$t_ref$L0 = null;

This is, I suppose, a slight disappointment – in VB6, the With block only had to dereference the object (meaning, look up its address) one time and now, presumably, it has to do so three times.  This is only a minuscule performance hit – hardly a major drawback.

Another interesting experiment is to consider a weakly-typed VB function.  When we refer to “strong typing” or “weak typing”, we are talking about the degree to which the compiler requires that variables be of the same type versus how much it will “infer” the proper type based on its value.  In other words, a weakly typed language distinguishes poorly between the number 5 and the string literal “5”.  When using VB, you should ALWAYS use the “Option Strict” directive, which makes the language into something more resembling a strongly-typed language.  But for purposes of this experiment, we are working with this option off.  So consider the function below.  If strNum is numeric, this function will execute “properly”.  (I use the term loosely since the “proper” thing for this to do would be to not compile.)

Function AddSomeNum(ByVal strNum As String) As Integer
Return 5 + strNum
End Function

The compiled version of this function is at least somewhat interesting:

public int AddSomeNum(string strNum)
return (int)Math.Round((double)(5.0 + Conversions.ToDouble(strNum)));

Even though both the number 5 and the return value are integers, this bit of weak-typed code converts everything to the type double.  If you were to call the function AddSomeNum(“1.9”), the answer would be 7.  This is not what one might expect, particularly if you have a C++ or C# background, as the usual result of converting a double to an integer is truncation, not rounding.

VB.Net may never be a “language for adults”, but if you do use it, turning on “Option Strict” is important, not only to prevent run-time errors (if you were to pass the value of “hello” to the above function, it would compile, but generate an error to the user).

Wanna talk more tech? Or wanna trust that we know what we’re talking about? Let’s talk!

Photo credit: k lee

Enjoy Your 30-day Free Trial!
inbound marketing software free trial Learn how Inbound Marketing Software can help your business grow!

Click here to get started with your free trial.

One Comment

  1. Sean Schroeder

    C++ is the mother of all modern languages, Perl, PHP, Java, etc have all inherited syntax from it. I would argue that it isn’t the language that is important. The first are the libraries and functions that it provides relevant to your project.

    For example you probably wouldn’t use PHP to write a stand alone application to run on a Windows system, but you would select ANSI C++ or Java due to the cross platform nature of both languages and the ability to compile to a standalone application.

    The second, and probably one of the reasons the Professor would say C/C++ are adult languages is the syntax. In VB it is designed to make it more user readable, for example in BASIC the GOSUB function is easier for a beginner to understand than SOMEFUNCTIONNAME() . Also, in languages such as C/C++ the programmer needs to comment the heck out of our code, but VB and other languages are “more human readable” requiring less commenting.

    Interesting that the title is “VB Livin’ in a C# World” both languages are from Microsoft, and one could argue that C# ( C Sharp) is just a natural evolution of VB into the .NET framework. Also, C#, the last I looked wasn’t ANSI C++, therefore it can only be ran and compiled on a Microsoft environment.

    I like programming discussions, even though I am a full time real estate agent I still have the Programming bug … Great article.

Comments are closed.