A 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:
.BackColor = Color.AliceBlue
.Checked = False
.Text = “test”
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.
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.)
Return 5 + strNum
The compiled version of this function is at least somewhat interesting:
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!|
|Learn how Inbound Marketing Software can help your business grow!|