Microsoft's .NET platform changes the face of Visual Basic programming. The first major step in the .NET strategy was the release of the new development suite, Visual Studio.NET. The development community has made an inordinate amount of noise about a new language called C# (C Sharp). But lost in this noise is a far more important piece of news: Visual Basic has received a complete overhaul. This article takes a look at the major changes introduced with VB.NET.
Each language that plays in the .NET world must target the Common Language Runtime (CLR). Think of this as similar to a VB runtime you have to distribute, only much bigger and with lots more cool toys. To target the common language runtime, VB had to undergo some radical changes, the most important of which is support for true object-oriented programming (OOP). Yes, you deal with objects in VB quite often, and some VB developers have even lived on the edge once or twice and stroked out the word "Implements" into the IDE, but VB6 did not meet the textbook definition of a language supporting OOP.
This is not to say that VB developers are by definition unfamiliar with OOP. But in the bigger picture, these developers are few and far between. The reality is that most VB programmers are largely procedural coders without a great deal of experience with object-aware technologies such as COM. That fact is what makes this bet a big one for Microsoft: Developer mindshare is the most valuable intangible in the technology business. The radical change that .NET brings to VB will open the door for alternatives like Java to steal that mindshare, but the vast majority will see the light and come to it.
Syntax and language are different
Learning the syntax of a programming language is often far easier than learning all the technical jargon that floats around it. This fact extends beyond individual programming languages into the larger domains such as Web programming and OOP. One of the most daunting tasks when making the move into the realm of OOP is understanding terms such as inheritance and overloading. Even a term such as class takes on a different meaning.
A class of its own
Classes are the core of OOP. Of course, VB has supported classes for some time now, but with VB.NET, things are a little different. For starters, classes begin and end with a code block instead of a physical file. Public Class MyClass….End Class defines a class; this allows you to hold multiple classes in one file. Second, you no longer have to “SET” a class; in fact, you can forget about the keyword SET. Forget about get and let for properties, as well; they’ve been replaced by a far smarter syntax. Most importantly, you must begin to think about your classes as virtual representatives of physical entities. The CLR is so good and so fast at object allocation that the overhead for creating an object is virtually nothing. One of the big drawbacks that kept developers from creating elegant object designs in VB was the fact that class creation/allocation was a drag. This ill has been cured.
Get overloaded—it’s a good thing
Developers often write a piece of code to satisfy some particular need. Over time, that piece grows to include more and more functionality. That growth sometimes brings with it a long list of parameters for the function. At times, you add considerable logic that makes execution decisions based on the parameters passed in, or you may even create new functions that do essentially the same thing. Numerous problems exist within this same domain, and these problems are only some of the headaches solved by method overloading.
Method overloading allows you to write multiple methods with the same name that are differentiated by the arguments passed to the method. When the method is called, the argument list is evaluated and the correct method is called. In this case, you would not have to write any logic to deal with the type of execution that should occur.
Initially constructing your final destruction
Special functions are called when classes are created or destroyed. You might be familiar with these from VB6, where they were called Class_Initialize and Class_Terminate procedures. While it was possible to add logic in these procedures, adding parameters was not allowed. The ability to conditionally provision resources for your class was not available at the point of creation. With VB.NET, these common methods, which are known as the class constructor and class destructor, can be manipulated to a further extent. The most useful change is the ability to add parameters to the New method, which is the replacement for Class_Initialize. This feature is known as “parameterized constructors” and can make the internal resource allocation of your class more efficient. Add to this the ability to overload the New method, and you have incredible flexibility.
Determine your own inheritance
Inheritance is a big subject; numerous books are devoted to it. For the purposes of this article, however, a simple example will suffice.
Imagine you are building an expense-reporting application. In an ideal world, you’d build classes to represent the physical entities in your system. Take an individual expense, for instance; it has numerous properties associated with it, so you might build a class called expense. Now let’s pretend there are three types of people who will use the application: employees, managers, and executives. It’s likely that for each of these groups, the business rules used to process these expenses are different. Even so, the basic properties will not change. The date of the report is the same regardless of who is issuing it, as with the expense amount and so on. This situation is ideal for the use of inheritance. We would create the expense class and consider it our “base class,” with some basic properties and methods.
We will inherit from that class when we create the ExpenseEmployee, ExpenseManager, and ExpenseExecutive classes. While this is a relatively simple example of the use of inheritance, it is the nucleus of the concept. You can also add code to your base class to keep other developers from manipulating members of your base class in the inherited class, or even make it so that your class cannot be inherited.
Regardless of what the tech pundits may say, Visual Basic has been a revolutionary language simply because of its ability to make very hard things much easier. Don’t let these language innovations fool you into believing it’s lost that ease of use. Visual Basic has only gotten better.