This Month's Hot Topic!!!

You have probably “noticed” how big Object-Oriented Programming (OOP) has become. Bunch of new books and technologies come out relying on OOP and declaring that OOP is the best way to program. I definitely agree with this idea. In this article, I’ll show how to create classes (the core technology of OOP), their functionality and usefulness.

Just for revision – OOP is based on creating classes and when the program is running, an instance of the class can be loaded and it becomes an object. Object gets its functionality from a class and can perform the methods the class has defined and implemented (in VB) and it also contains member variables the class defined. You can have many instances of one class running (many objects).

How To Create a Class

Let’s make a simple application and look closer at how classes work. Run your Visual Basic, and choose New Standard EXE Project. To add a class to the project, go to Project menu and click on “Add Class Module”. Select “Class Module” and click “Open”. This will add a simple class module to your project. Close the code window, and rename the class that we just added, to CTest. I use C prefix in front of the name of the classes. But I have seen other programmers use T (as type) or cls prefixes.

Once you added a class, you can add code to it. Any functions and/or procedures that you add to a class are called methods in OOP terms. And any variables you add to a class are called member variables.

Running and Adding Code to a Class

A class becomes an object when at run-time your program executes the following line of code:

            Set reference_to_class = New Class_Name

Reference_to_class is a variable you declare in your form and Class_Name is the name of a class you want to use. For example, to use Ctest class we would write this:

            Dim clsCTest as CTest
            Set clsCTest = new CTest

Previous two lines of code load an object of type CTest. Don’t forget to destroy this object once you are done with it:

            Set clsCTest = Nothing

Of course, right now, CTest is useless since it contains no code. Let’s add some methods to CTest. To do this, double click on CTest icon in Project Explorer add some code

Image 1 - The code of CTest

Private intCounter as Integer – declares a variable which will keep count of how many times we call the method OutputText.

Class itself has two methods, Initialize() and Terminate(). These methods are called automatically (you don’t call them). Class_Initialize is called when your program executes the line “Set reference_to_class = New Class_Name”. It gives you a chance, as a programmer, to do some initialization work before the object can be used. In this example, as you can see, I told to output some message, and also initialized my variable intCounter. Class_Destroy is called when your program executes the line “Set reference_to_class = Nothing”.  It gives you a chance to do any clean up work you need to do before the object is destroyed.

I also added my own method – OutputText. It outputs to the debug window the message informing you how many times you called this method. To use this method, you have to call it from your form where clsCTest is declared like this: clsCTest.OutputText(). For example, I added a command button to frmMain, and whenever the user clicks on this button, OutputText method of CTest is called. Here is frmMain code

Picture 2. frmMain code

Run the program, and click couple times on the command button. In the debug window you should see “I’m initialized” and several “You called me n time(s)” messages. When you close the program, “I’m destroyed” will be output.

That’s just very basic functionality of a class. You can add more complicated code to do more useful work than just outputting something (like accessing database). There is also some more things which I haven’t discussed about classes, like events and Property procedures, but that’s for the next article

Topics - Home

Made By Laimonas Simutis. 2001. laijerrad@yahoo.com