Object Oriendted Programming with VB (part 2)
In the first article the concept of a class was introduced. Now I'm going to show you several more advanced concepts of a class, mainly: properties and events.
Properties and Events. What is that?
The best way to explain what properties are is using real life examples. Let's take a human eye for example. How would you descibe it? Most likely by telling what color it is and is it big or small. So color and size are the properties. The same concept applies to programming. Let's say we have a class whose object represent a certain file. Each file has its size, name, type. So these three things would be properties that we would give to a class. Why do you need properties? Well, it makes classes easier to use as well as it makes a class more descriptive. You sort of make a distinction between class' functions and properties. There are things that class can do (functions aka methods) and describe (properties). Enough of "theory", here is the example of a property color declared inside the class:
Dim sColor As String
Public Property Get Color() As String
Color = sColor;
Public Property Let Color(ByVal sNewColor As String)
sColor = sNewColor
As you can see, in order to keep properties, internal private variable is needed of the same type (in this case, string.) Well type must not be of the same type, but why would you want to cast or convert? Keywords Get and Let are key here. If you want to declare read-only property, erase Let method. If you need write-only property, erase Get method. Accessing properties is very easy. For example to set the color to blue of refEye object, this line would be used: refEye.Color = "blue".
Now events. What are events?. Events help object communicate with the program that instantiated an object.
Events are sent to program by calling RaiseEvent method inside the object. For the program to receive the events, it must use special declaration keyword WithEvents. Here is a short example:
Private WithEvents refFU As FileUtil
Private Sub cmdEnumFiles()
Set refFU = new FileUtil
Set refFU = Nothing
Private Sub refFU_EnumFile(ByVal iNumber As Integer)
What you see in this example is a simple VB EXE program declaring refFU together with WithEvents keywords. When FileUtil object finds a file it raises an event called EnumFile and passes the current number of files enumerated. The events call the function also defined in the VB EXE program using a simple convention: objectname_eventname (in our example 1 that was refFU_EnumFile). Here is what happens in the class. First declare an event:
Public Event EnumFile(ByVal iNumber As Integer)
Now here is our enumFiles method:
Public Sub enumFiles(ByVal dir As String)
.... do some stuff
RaiseEvent EnumFile(iNumber) 'indicate that file found
Pretty nice ah? One thing to mention about raising events. Events are asynchronous, meaning that the object will not wait for the program to process the event to do its job. It will continue on right after event was raised.
Play around with events and properties. They are essential in object oriented world. Once you get used to the concepts they are easy and nice to use.
Written By Laimonas Simutis. 2002.