Using Class Modules in VB6 We can use class modules in one or more Visual Basic application. The class module can be contained within a compiled ActiveX EXE or ActiveX DLL. In this case, the class module would not be added to the project. Instead, a reference would be made to the ActiveX EXE or ActiveX DLL. After the reference has been set, we can create objects from classes in the component and write code using the object's properties, methods, and events. In this chapter, we will focus on creating class modules for use within a single Visual Basic project. For information about using class modules in COM components, see Chapter 8.
Adding A Class Module to a Project Before we can use a class module, we must first manually add a new class to the project, or load an existing one.
*To add an existing class module to a project *On the Project menu, click Add Class Module. *The Add Class Module dialog box appears. *Click the Existing tab. *Browse for the Class Module (.cls file) that we want to add to the project, then click Open.
After we add the class to our project, we must declare an object variable to store the object. We declare an object variable in the same way we declare other variables, with Dim, ReDim, Static, Private, or Public.
Declaring Object Variables In most cases, we know at design time the type of object we want to create and use in our application. It is much more efficient, in these cases, to use specific object variables to point to the objects we create. A specific object variable refers to a particular object type and can only hold pointers to that type. If we try to store a different object type in that variable, an error will result.
For example, we can declare any type of Visual Basic object, such as a form or control and then assign values to these variables:
Dim frm As Form Dim ctl As Control
Set frm = Form1 Set ctl = Command1
However, a run-time error will occur if we try to assign the wrong type of object to the object variables:
'These two lines of code will cause a run-time error Set frm = Command1 Set ctl = Form1
Once we've added a class module to a project, we can create an object variable based on its type. In the following example, a House object variable is created based on the CHouse class:
Dim House as CHouse
Using External Objects In some cases, we might want to declare object variables from external sources, such as Excel, Word, or Internet Explorer. Before we can use an object external to Visual Basic or our application, we must set a reference to that object's type library. A type library contains standard descriptions of exposed objects, properties, and methods that are available from a software component. The files that contain type libraries are called object library files, which have a .olb extension.
If a reference has been set to the object's type library, Visual Basic detects an object variable at design time when we write code for the client application. Visual Basic can display information about the available methods and properties, as well as check the syntax of each method or property call. Another advantage is that we can use the Visual Basic Object Browser to view information about the object's methods, properties, and events.
*To set a reference to an object library in Visual Basic *On the Project menu, click References. *Select the object library to reference, then click OK.
The following example declares a variable called "ie" in the General Declarations section that will hold a pointer to Microsoft Internet Explorer objects only:
Dim ie As InternetExplorer
Generic Object Variables There are cases when we do not know at design time the specific type of object our application will use. In these situations, we can use generic object variables to hold pointers to any type of object. For example, we might want to write a function that acts on any one of several different classes of objects. In this case, we must declare the variable As Object.
The following example uses the Object data type to declare a generic object variable:
Dim objGeneric As Object Since the specific object to be used will not be known until run time, we cannot set a reference to a library for the object in Visual Basic. we should only use generic variables when absolutely necessary since they have several disadvantages:
Creating Objects from Components Once we have made the component available to our application and have declared an object variable to hold an instance of the component, we can create the object.
In Visual Basic there are three ways to create an object to access an component:
*Use the New keyword with a Set statement. *Use the GetObject function. *Use the CreateObject function.
we instantiate, or create, an object from a class using one of these statements or functions. Therefore, to use an object, we must declare the object variable, then create an instance of the object. An object variable is a pointer to a location in memory where an object will be stored. Once the object variable is instantiated, the memory location will contain a newly created object.
Note: -------------------------------------------------------------------------------- This chapter will focus on using the New keyword to create instances of a class. The CreateObject and GetObject functions will be covered in detail in Chapter 8.
Using the New Keyword with the Set Statement Whenever we assign a value to an object variable in Visual Basic, we must use the Set statement. The Dim, Private, Public, ReDim, and Static statements only declare a variable that refers to an object. No actual object is referred to until we use the Set statement to assign a specific object.
Generally, when we use the Set statement to assign an object reference to a variable, a new object is not created for that variable. However, when we use the New keyword in the Set statement, we are actually creating an instance of the object. The following example shows how to use the New keyword with the Set statement to create an instance of the CHouse class:
Sub CreateHouseObject() Dim House As CHouse Set House = New CHouse End Sub
we can also use the New keyword in a Dim statement to declare an object variable. If we use New when declaring the object variable, a new instance of the object is created on first reference to it, so we don't have to use the Set statement to assign the object reference. Avoid using variables declared using the New keyword because it can slow our application. Every time Visual Basic encounters a variable declared using the New keyword, it must test whether or not an object reference has already been assigned to the variable.
Summary Class modules give the capability to create reusable objects through ActiveX EXEs and DLLs. we can also use class modules to create objects within a single application. If the class module has been included in a compiled ActiveX EXE or DLL, we can set a reference to the component in the Visual Basic IDE. After the reference has been set, we can create objects from classes in the component and write code using to use the object.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|