Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
(Publisher: The Coriolis Group)
Author(s): Peter G. Aitken
ISBN: 1576102815
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


But now, let’s get back to the client program. In the form’s General declarations section, we create a reference to the ActiveX class by declaring a variable of that type, as shown here:

Private Ob1 As New AXDEMO

Note the use of the New keyword, which is necessary, because we want to create a new instance of AXDEMO to which the variable Ob1 will refer. Without New, we could make Ob1 refer to only an existing AXDEMO object, using the Set statement. Of course, that existing instance of AXDEMO would need to be created earlier with New. Note also that the AXDEMO class is available in the Auto list that Visual Basic displays after you type the As keyword. This is an excellent demonstration of how Visual Basic is well integrated to make your life as a programmer as easy as possible.

Declaring a variable of type AXDEMO does not actually create an instance of the class. This occurs the first time a property or method of the class is referenced. We’ll do this in the client app’s Form_Load procedure by using the ChangeName method to assign a name to the server’s Name property:

Private Sub Form_Load()
    Ob1.ChangeName(“MyObject”)
End Sub

The one line of code in this procedure calls the ChangeName method in Ob1. As we saw earlier, Ob1 refers to an instance of AXDEMO. Code in the ChangeName procedure assigns the argument (in this case MyObject) to the private variable objName.

Having the object perform the calculations is a simple matter. Clicking on either the Cube Root or Square Root button assigns the number in the first Text Box to the object’s Number property. Because the code that performs the calculations is located in the Number property’s Let procedure, it is performed automatically each time this property is changed. We can then immediately read back the answer from the appropriate object property—either CubeRoot or SquareRoot.

To obtain information about the object, we access its Name and Created properties, placing them together in a string that is displayed with the MsgBox procedure. To terminate the program, we destroy the AXDEMO object by setting Ob1 to the special keyword Nothing, then call End to terminate. Listing 8.5 shows the code for the Click event procedure for the array of Command Buttons.

Listing 8.5 The Click event procedure for the demo program’s Command Buttons.

Private Sub Command1_Click(Index As Integer)

Dim Msg As String

Select Case Index
    Case 0  ‘ Square root
        Ob1.Number = txtInput.Text
        txtOutput.Text = Ob1.SquareRoot
    Case 1  ‘ Cube root
        Ob1.Number = txtInput.Text
        txtOutput.Text = Ob1.CubeRoot
    Case 2  ‘ Information
        Msg = “Object ” & Ob1.Name & “ was created ” _
            & Ob1.Created
        MsgBox (Msg)
    Case 3  ‘ Quit
        Set Ob1 = Nothing
        End
End Select

End Sub

That’s all there is to the client app. Run it, enter a value in the first Text Box, and click on one of the calculation buttons. Because the program has no error checking, be sure to enter a positive value—no negative numbers, please. Click on the Information button to see the object’s name and creation time/date.

What if you need to make changes in the DLL code? Switch to the first copy of Visual Basic and make the changes. Be sure to execute the DLL within Visual Basic (to register any changes) and to use the Make command on the File menu to create the DLL file on disk. Then you can switch to the second copy of Visual Basic and run the client program again to see the effects of your changes.


TIP:  Overcoming The DLL-To-Disk Hurdle

After testing your client program and switching back to modify the DLL, you may find that you are unable to write the modified DLL to disk. This problem may be solved by the time you read this, but you can get around it by closing the second copy of Visual Basic (the one you are using to develop the client program) before trying to make the DLL. Then restart the second copy of Visual Basic to continue with your testing.


Registering Components

As you have seen, for an ActiveX component to be available to other programs, it must be registered with Windows. If you run an ActiveX server from within the Visual Basic environment, it is automatically—but temporarily—added to the Windows registry. When you terminate execution, the registry entry vanishes. If you “make” the project, creating an EXE or DLL file, the server and its classes are permanently added to the registry. But what about end users who are not running Visual Basic? For them, the class is registered when it is installed by using the Setup tool kit, or when it is executed as a regular EXE file. It can also be registered using the Windows REGSVR32.EXE utility.

Creating An ActiveX EXE

We have seen how to create an ActiveX component contained within a DLL file. You could include more than one ActiveX class in a single DLL simply by adding additional class modules to the project. But what about an ActiveX EXE? As I mentioned earlier in the chapter, an ActiveX EXE combines one or more ActiveX components with a functional program. In almost all cases, the functional part of the ActiveX EXE makes use of its own ActiveX components as well as exposing them for use by other programs. How do you do this?

It’s really quite simple. I won’t walk you though a complete example, but I will describe the process using our earlier demonstration programs for examples. Suppose you want to combine our ActiveX server with the client demonstration to create an ActiveX EXE that does the following:

  Defines a class to calculate square and cube roots
  Exposes this class for use by other programs
  Provides a visual interface permitting the user to enter data and read the result of the calculations

You can easily accomplish these tasks by creating an ActiveX EXE project. The project will have two modules: a class module containing the same code as the class module in our DLL example, and a regular Visual Basic form module with the same controls and code as the client example. In this case, the class and its client are part of the same Visual Basic project, and the class is also available (once it has been registered) for use by other Windows programs.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.