In first three lessons, you have learned how to enter the program code and run the sample Visual Basic 2012 programs but without much understanding about the logics of Visual Basic 2012 programming. Now, let's get down to learning a few basic rules about writing the Visual Basic 2012 program code.
First of all, Visual Basic 2012 is very much similar to Visual Basic 2010 and Visual Basic 2008. However, it is very different from Visual Basic 6. While they are nearly similar in terms of Interface and program structure, their underlying concepts are quite different. The main difference is that Visual Basic 2012 is a full fledged Object Oriented Programming Language while Visual Basic 6 may have OOP capabilities, it is not fully object oriented. In order to qualify as a fully object oriented programming language, it must have three core technologies namely encapsulation, inheritance and polymorphism. These three terms are explained below:
Encapsulation refers to the creation of self-contained modules that bind processing functions to the data. These user-defined data types are called classes. Each class contains data as well as a set of methods which manipulate the data. The data components of a class are called instance variables and one instance of a class is an object. For example, in a library system, a class could be member, and John and Sharon could be two instances (two objects) of the library class.
Classes are created according to hierarchies, and inheritance allows the structure and methods in one class to be passed down the hierarchy. That means less programming is required when adding functions to complex systems. If a step is added at the bottom of a hierarchy, then only the processing and data associated with that unique step needs to be added. Everything else about that step is inherited. The ability to reuse existing objects is considered a major advantage of object technology.
Object-oriented programming allows procedures about objects to be created whose exact type is not known until runtime. For example, a screen cursor may change its shape from an arrow to a line depending on the program mode. The routine to move the cursor on screen in response to mouse movement would be written for "cursor," and polymorphism allows that cursor to take on whatever shape is required at runtime. It also allows new shapes to be easily integrated.
VB6 is not a full OOP in the sense that it does not have inheritance capabilities although it can make use of some benefits of inheritance. However, VB2012 is a fully functional Object Oriented Programming Language, just like other OOP such as C++ and Java. It focuses more on the data itself while VB6 and earlier versions focus more on the actions. VB6 and its predecessors are known as procedural or functional programming language. Some other procedural programming languages are C, Pascal and Fortran.
Visual Basic 2012 allows users to write programs that break down into modules. These modules will represent the real-world objects and are knows as classes or types. An object can be created out of a class and it is known as an instance of the class. A class can also comprise subclass. For example, apple tree is a subclass of the plant class and the apple in your backyard is an instance of the apple tree class. Another example is student class is a subclass of the human class while your son John is an instance of the student class.
A class consists of data members as well as methods. The following examples define a human class and a car class
Public Class Human 'Data Members Private name As String Private ID String Private dateofbirth As String Private gender As String Private age As Integer Private designation as String 'Methods Overridable Sub ShowInfo( ) MessageBox.Show(name) MessageBox.Show(ID) MessageBox.Show(dateofbirth) MessageBox.Show(gender) MessageBox.Show(age) MessageBox.Show(designation) End Sub End Class
Public Class Car 'Data Members Private Brand As String Private Model As String Private YearMade As String Private Capacity As Integer 'Methods Overridable Sub ShowInfo( ) MessageBox.Show(Brand) MessageBox.Show(Model) MessageBox.Show(YearMade) MessageBox.Show(Capacity) End Sub End Class
Let's look at one example on how to create a class. The following example shows you how to create a class that can calculate your BMI (Body Mass Index).
To create class, start Visual Basic 2012 as usual and choose Windows Applications. In the Visual Basic 2012 IDE, click on Project on the menu bar and select Add Class, the Add New Item dialog appears, as shown in the Figure4.1
The default class Class1.vb will appear as a new tab with a code window together with the defaut form Form1.vb, as shown in Figure 4.1. Rename the class as MyClass.vb. Rename the form as MyFirstClass.vb.
Now, in the MyClass.vb window, enter the following code
Public Function BMI(ByVal height As Single, ByVal weight As Single) BMI = Format((weight) / (height ^ 2), "0.00") End Function
Now you have created a class (an object) called MyClass with a method known as BMI.
In order to use the BMI class, insert a button into the form, change its name to BtnCalBmi and its text to Calculate BMI. Next, click on the button to enter the following code:
Private Sub BtnCalBmi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCalBmi.Click Dim MyObject As Object Dim h, w As Single MyObject = New Class1() h = InputBox("What is your height in meter") w = InputBox("What is your weight in kg") MessageBox.Show(MyObject.BMI(h, w), "Your BMI") End Sub
When you run this program and click the Calculate BMI button, the user will be presented with two input boxes to enter his or her height and weight subsequently and the value of BMI will be shown in a pop-up message box, as shown in Figure 4.3.
After entering your height and your weight, the pop-up message box will display your BMI reading, as shown in Figure 4.4