XTreme Logo

If you appreciate these tips, please consider a small donation:

ActiveX Tip - Intermediate

On instancing and naming.
Applies to: Visual Basic 5.0
Courtesy of Richard Mageau

The Global Multi-use object is a new Instancing property for class modules in VB 5. This allows you to create your own Global type objects!  Typically, you would create an instance of your ActiveX server by using the following notation:

Dim X As MyServer.MyClass

But to dimension a Recordset, all we had to do was:

Dim X As Recordset

The DAO Engine is a global multi-use component. Therefore, we don't need to say DAO.Recordset to dimension a recordset type object. The DAO prefix is implied!  So if we create a global multi-use class in our server, all we have to do is:

Dim X As MyClass

CAUTION!  If you have to use class names that are the same as currently existing objects, global multi-use objects WILL give you both type Mismatch errors and peptic ulcers.

Let's say we create an ActiveX component (called MyServer). In it is a global multi-use object called TableDef.  Now, if we go to the General Declarations section and dimencion a variable like this:

Dim X As TableDef

Which tabledef object will VB think of? (Also notice that the Quick Help shows 2 tabledef objects available).

Also, if you create a routine and pass a parameter as tabledef, which one do you think VB will think it is? (Choose wisely and it will work, choose poorly and feel the wrath of Damacles!).

Best bet, if you are going to use names that may be the same as something else in a project, or if you are writing a commercial ActiveX component, please make the parameters you pass to a routine specific (Like x as MyServer.MyClass). This is so that type mismatches will solely be the fault of the developer who is trying to use your server and not a bug in your project!

 


[ Back To The Top ]

Contact: web@xtremecomp.com
All contents copyright XTreme Computing unless noted otherwise.