VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering

You are here: Visual Basic > Advanced VB6 tutorial > Chapter 12

Manipulating Class Object Properties and Methods

After you have instantiated your Class object, manipulating the object's properties and methods becomes quite straightforward: You use exactly the same techniques and syntax as you would use to manipulate VB's standard objects.

You manipulate a Class object's properties with the Object.Property syntax, as in the following example:

   objMy.Name = "Vergilio"
   MsgBox objMy.Path

You manipulate a Class object's methods with the Object.Method syntax, as in the examples in Listing 12.11.


   'Method without argument or return value
   'Method with argument
   objName.KillFile "MY.BAK"
   'Method with return value
   strDir = objName.FindFile("VB.EXE")

Handling a Class Event

You handle a Class object's event just as you would handle events for a VB control: by writing code in the predefined stub of its event procedure.

To make the object's event procedure available in a form or another class module, you must declare the object variable with the special WithEvents keyword in the General Declarations section, as in the following example:

   Private WithEvents FF As FileFind

where FF will be the name of the object variable, and FileFind is the name of its class.

After you insert this declaration in your code, you will be able to see the object variable in the Objects list box at the upper-left corner of the code window of the file (Form or Class) where you put the declaration. When you choose the object from this list, you will then be able to see the object's events in the Events list box at the upper-right corner of the code window. If you use the Events list box to navigate to the event you are interested in, you will find your cursor blinking inside the predefined event procedure stub for that event, just as you would for any control you had placed on a VB form (see Figure 12.5).

At this point, all you need to do is to write code in the event procedure, as in Listing 12.12. If the event procedure furnishes parameters, you can use them and, if appropriate, change them. The event procedure in Listing 12.12 takes three parameters. The first two are informational, and the third (Cancel) could be changed to True to tell the object to stop the process that is generating these events (in this case, a file search process).


   Private Sub FF_FileFound _
      (strFileName As String, _
      strFilePath As String, _
      Cancel As Boolean)
      iFilesFound = iFilesFound + 1
      If iFilesFound [me] 20 Then
         'tell object to stop processing if
         'we found the maximum number of files
         Cancel = True
      End If
   End Sub

Navigating to the event procedure for a custom object's event.
FIGURE 12.5 Navigating to the event procedure for a custom object's event.

WARNING - Can't Use As New in WithEvents: Declaration When you declare an object using the WithEvents keyword, you can't instantiate it at the same time with the New keyword. You must instantiate the object later in your code.


<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved