When Microsoft launched Visual Basic .NET (VB.NET), it appeared to many to be just another version of the ‘old’ Visual Basic 6.0 with a few added features. Once they started using it, they found that the new language provided healing power for all the shortcomings of VB 6.0.
VB.NET is a unique and easy to learn language based on Microsoft’s .NET framework. The .NET framework is a software framework that includes a large collection of libraries, which helps in development of more robust applications. It includes a virtual machine (CLR) that manages the execution of programs. Microsoft’s .NET framework supports many languages – and crucially, it allows language interoperability, whereby each language can use code written in other languages.
Common Language Runtime (CLR) is a runtime environment provided by the .NET framework. It manages the code during execution, and provides benefits like structural exception handling, enhanced security, versioning, automatic memory management, very strong type safety, language interoperability and enhanced performance.
VB.NET is gaining popularity, as it supports object-oriented constructs such as abstraction, encapsulation, inheritance and polymorphism.
Object Oriented Programming (OOP) is a programming concept based on objects and classes. It represents the best methodological framework for software development. Programming with objects is like working with real-world objects. An object has specific ‘attributes’ (that is, data members) and ‘behaviours’ (methods).
Using attributes and behaviours
Say we have a class called Employee, which has attributes (data members) and a behaviour (method).
Public Class Employee
Private salary as Double;
Private designation as String;
Public Function CalculateSalary() as Double
‘write code for calculating salary
This Employee class has two data members - salary and designation - which describe the attributes of an Employee. It also has a method, CalculateSalary, method which describes the behaviour of an Employee.
Encapsulation and Abstraction
Classes in VB.NET include the concepts of ‘encapsulation’ and ‘abstraction’. Abstraction means to show only essential features to outside world – so that you can understand what a programming element does, without having to necessarily understand exactly what goes on inside it. Encapsulation means storing both data and methods in one unit (Class).
For example, our Employee class encapsulates both data members and methods. Only the CalculateSalary method is exposed, hiding other non-essential data from outside world: this is abstraction.
VB.NET is now equipped with the implementation of inheritance so that a ‘child’ class can now inherit attributes and behaviours from its parent class.
For example, we have a Manager class that is inherited from the base class Employee
Public Class Manager: Inherits Employee
Private bonus as Double;
‘this is method 1
Public Function CalculateBonus() as Double
‘write code for calculating bonus
Instances of Manager Class will contain both methods, CalculateBonus and CalculateSalary.
VB.NET also supports Polymorphism. This means “one name, many forms”. We implement polymorphism by Function Overloading and Function Overriding.
Function Overloading, means creating more than one method with same name but a different ‘argument list’. The function performs different operations, based on the argument passed to it in the function call.
‘this method 2 is added in Manager class
Public Function CalculateBonus(ByVal newBonus As Double) as Double
‘write code for calculating bonus
Now our Manager class contains methods that share the same name (CalculateBonus), but which have a different argument list. The first method does not take any argument, while the second method takes newBonus as an argument. When a user calls this method without supplying any argument, then method 1 will execute. If there’s the argument Double is passed, method 2 will execute.