Object Oriented Programming for VB.NET - Part 1by Budi Kurniawan
If you ask an OOP guru what an OOP language is, you will probably hear him/her utter words such as class, interface, information hiding, encapsulation, inheritance, polymorphism, etc. Those sound cool, don't they? OOP, however, is a topic that is not too easy to master in a day or two or by just attending a lecture. To truly understand it, you need to know the theory as well as do some OOP coding practice. This article offers readers just getting into OOP the very basics of OOP, using VB.NET as the language. It is not a complete reference, but it is something to start with.
The Benefits of OOP
Have you wondered why modern programming languages tend to be object-oriented? C++ was created as an extension of C to support OOP. And Java, one of the most popular languages on the planet, is also an OOP language. Then, of course, VisualBasic has evolved into VB.NET, a fully OOP language. There is a good reason for this transformation. OOP offers several benefits, such as easy code maintenance, extensibility, and code reuse, not found in procedural programming languages. Some of the benefits are outlined below.
- Easy Maintenance. Modularity is inherent in OOP. Entities are represented by classes and classes with the same functionality are located in the same namespace (package). You can add a class into a namespace without affecting other members of the namespace.
- Extensibility. OOP naturally supports extensibility. Having a class with certain functionality, you can quickly extend that class to create a different class with extended functionality.
- Code Reuse. Since functionality is encapsulated into a class and each class is an independent entity, providing a library is very easy. In fact, programmers of any .NET Framework language can and should use the .NET Framework class library, an extensive library that provides rich functionality. Better still, you can extend the functionality of the class library to provide classes that suit your needs.
Now, let's look at some of the features of OOP, starting with the easiest one, classes.
Classes are the main focus in OOP. Simply put, a class is a data type that provides some functionality. A class in VB.NET is declared using the keyword
Class. For example, Listing 1 presents a class called
Listing 1: Class Employee
Class Employee End Class
As simple as that. Note that Microsoft recommends the use of Pascal-case naming for classes. This means the first character of a class name is uppercase, and so is the first letter of any subsequent concatenated words. Good class names according to this convention include
StringUtil, and so on.
|Do you find OOP important to your Visual Basic programming needs? Give some examples.|
A class can have members such as fields, properties and subroutines and functions. For example, Listing 2 presents the Employee class with a subroutine called
Listing 2: Class Employee with method Work
Class Employee Public Sub Work () ' Do something here End Sub End Class
Subroutines and functions are both called methods. Method names are also Pascal-case.
Another type of class member is a field. A field name is camel-case, which means you capitalize the first character of each word except the initial character. Examples of good field names are
quarterlyBonus, etc. Adding two fields called
yearlyBonus to the
Employee class in Listing 2 will result in the following code.
Listing 3: Class Employee with two fields
Class Employee Dim salary As Decimal = 40000 Dim yearlyBonus As Decimal = 4000 Public Sub PrintSalary() ' print the salary to the Console System.Console.Write(salary) End Sub End Class
Instantiating an Object
A class is a template or a blueprint of the entity that the class represents. To use a class's field or method or any other member, you need to first turn the class into an object, just as the blueprint of a sports car is useless until assembly workers make the car. In other words, you drive a car, not the blueprint of a car.
In OOP, an object is said to be an instance of a class. Creating an object is therefore known as instantiation. The code in Listing 4 demonstrates the instantiation of the
Listing 4: Instantiating an object
Class Employee Dim salary As Decimal = 40000 Dim yearlyBonus As Decimal = 4000 Public Sub PrintSalary() ' print the salary to the Console System.Console.Write(salary) End Sub End Class Module Module1 Public Sub Main() Dim anEmployee As Employee anEmployee = New Employee() anEmployee.PrintSalary() End Sub End Module
Module1 in Listing 4 provides the
Main sub, which is the entry point of a VB.NET program. In order to compile, a source file must provide access to the
Main sub in one way or another. If you are not using Visual Studio.NET, you can compile your VB.NET program with vbc.exe, which is automatically installed when you install the .NET Framework. For instance, if you save your source code as Employee.vb, go to the directory where the file Employee.vb is stored, and then type
Now back to the code. The
Main sub first declares an object variable of type
Employee. The variable is called
Dim anEmployee As Employee
It then instantiates Employee with the keyword
anEmployee = New Employee()
Now that you have an object of type Employee, you can use its functionality. (Once the car is built, you can start and drive it.) In our example, we can call the
PrintSalary method as follows.
This will print the value of the
salary variable in
You can also move the
Main sub into the class. This way, you don't need a module. This approach is shown in Listing 5.