In this discussion, we will talk about object oriented programming in vb.net. In the Object Oriented terminology, we will talk about abstraction and encapsulation in the context of Visual Basic. The concepts may be useful to many coders, but I have presented the content with beginners in mind. We start with creating a class in Visual Studio and then write some code while discussing various concepts like classes and objects, access types (public, private and friend), properties (and paramterized properties), methods, events, instantiation, enumerations and so on.
Preparation
- Start Visual Studio and create a new Windows Application
- Add a class; Project | Add Class…
- Give a suitable name to the class file (I have used cStudent.vb in this discussion)
We are going to use this file cStudent.vb to create our class and the Windows Form Form1.vb to create object & use the code written in class.
Defining a class
End Class
To define a class we simply have to use the Class keyword. For example, to create our cStudent, we may use Class cStudent. However, if we add the class module in Visual Studio, this code (at the right) comes by default in the class file. But wait, what is this ‘Public’, this is called access specifier or access type.
Access Types
Private Class cChild
End Class
End Class
Private: If a class is declared with private, it is accessible only within the class in which it is defined. Yes, we can define one class in another in vb.net. Also note that we can use private modifier only when there is some parent class because otherwise it is meaningless to use it.
Friend: – If we declare a class with friend modifier, it means that the class is accessible within the program, but not outside it. This means that if a class is declared with Friend keyword, programs other than the one in which this class is defined, will not be able to use it. This is the default access type, that is, if you do not specify an access type, the class will be treated as friend.
Public: – No restrictions. The class declared with public keyword can be accessed from anywhere.

Graphical Representation of Access Types
Back to Defining a Class
Now we know the syntax to declare a class. A class represents an entity, as we know. An entity has data and behavior associated with it and in our code this is done through properties, methods and events that we can create within our class.
A class is nothing but virtual representation of a real world entity. When we write this student class, we are defining a student! Once this definition is complete, we can create as many students as we want (instances of student class – objects) and program any scenario in which students are involved. Each student will have its own life. (Perhaps God created us like this and we are nothing but instances of class Human…. Matrix!!)
Public Sub Laugh()
MsgBox(”:)”)
End Sub
End Class
Okay, let us write a simple method (Laugh) in our class and use it. We have just taught our student to laugh. Remember this definition is not the student, this is just the definition for a student, that is, it just defines how a student should behave. So, all students created using this definition will be capable of laughing.
As said above, cStudent is just the definition of class Student. How exactly can we create a student (object) from this class? Well, let us go ahead and put a button on our form called Button 1 and write the code (see right) in the button’s click event procedure.
Dim objStudent1 As cStudent
objStudent1 = New cStudent()
objStudent1.Laugh()
objStudent1 = Nothing
End Sub
The four lines of code in the click handler do the following tasks respectively:
- Line 1: Declaration
- Line 2: Instantiation
- Line 3: Calling Method
- Line 4: Dereferencing
Declaration
When we create a student object, we would definitely want to access it. How would you access the object as it will be created in memory? The answer is that we will access the object through a name (reference variable) which will actually be a pointer to the object. The first line tells VB.NET that we want our object variable to be able to point to an object of cStudent (note that there is no object instantiated till this point). objStudent1 is a variable which can point to an object of cStudent class and no other class (except the child classes).
Instatiation
New keyword is very important in VB.NET. It is used to create an object (in memory) of the specified class (using a specified constructor, to be discussed later) and return its reference. In this line, new keyword creates an object of class cStudent and returns its reference (memory address) to objStudent1. We have already declared objStudent1 as a variable which can hold reference of type cStudent. Now we can access the object through the name/variable objStudent1. This is called instantiation.
These two lines can be combined into one:
Dim objStudent1 As New cStudent
Calling Method
In third line we are calling Laugh method of the object through our reference variable objStudent1. Remember this method is there in the object because it was defined in the class of which this object is an instance. Similarly we can access any member (property, method, etc.) defined in a class using object variable name (objStudent1 in this example) followed by a dot (.) and member name.
Dereferencing
Till this point, our reference variable objStudent1 holds reference of the object. This line assigns Nothing to objStudent1, that is, it makes it point to nothing! This makes our object inaccessible (can’t access this object now as objStudent1 points to something else and no reference variable refers to this object) and such objects (which are of no use as we cannot access them) are removed from the memory automatically (garbage collection). It is a good practice to dereference the object after they have served their purpose so that they can be removed from the memory.

Steps 1 to 4 - Object Life Cycle
You may read the rest of the content (unedited) here. The topics covered there are Properties, ReadOnly and WriteOnly Properties, Parameterized Properties, Enumerations, Default Properties, Methods, Optional Parameters, Enum and Object as Parameter, Shared Members, Events, etc.


Comments