Simple VB 6 Question.

This is my first shot at creating .dlls and .cls files. I have done a number of .exe so I’m not a total newbie.

How to explain……

My project (Call it CustomTool.vbp) has a form and a number of classes in it. The class files all have the same properties. There are private and PUBLIC properties and procedures in all of the .cls files. In fact most of the files are copies of each other with some changes in them.

Lets look at two class files. Button.cls and Tool.cls

If I go into any sub procedure in either of these files (or any of the other ones for that matter) and type in “CustomTool**.**” I only get the auto complete for “Button” and “form1”. And any of the sub procedures in Button and form1. I get nothing for Tool (or any of the other class files).

In fact, Button.cls and form1 are the only files seen by any of the other .cls files. I have gone so far as to copy Button into a new .cls file. But still, the only file that is ‘seen’ is the original.

Let me try to make a simple example –

Button.cls –
Public Sub dothis()
Call Tool.dothat <<<< Fails at compile with “Variable Not Defined”
Msgbox (“do this”)
End Sub

Tool.cls
Public Sub dothat()
Call Button.dothis <<<<Works fine.
MsgBox (“do that”)
End Sub

I must be doing something simple wrong, But I’m getting so I don’t see the forest, just the trees.

  • Thanks

Kinda guessing since I haven’t seen the code, but:

Classes don’t work quite like forms. Before you can call their methods you must create an instance.

For example:
Button.cls



Public Sub dothis()
    Dim objTool as Tool
    Set objTool = New Tool
    objTool.dothat
    Msgbox (“do this”)
End Sub


This procedure declares a variable (objTool) which is of a type to contain an instance of a Tool class. It then creates an instance of a Tool class ("…New Tool") and assigns it to this variable. The “Variable not defined” error is being thrown because there is no “Tool” variable in the procedure.

As for the intellisense failing, this is usually due to some other problem that would halt the compilation of the project. Try doing a Start with Full Compile (Ctrl+F5) and see if throws anything up.

I really don’t think it’s a problem with declaring variables. I’m not even trying to get at a method (in this case), so I shouldn’t have to create an instance of it. Something else is going on here. If you look at my simplified version (two simple sub procedures) “Tool”’s intellisence can see Button. It can find the procedure dothis() and it runs fine.

On the other hand “Button” can’t see “Tool” when it executes it can’t figure out that dothat is a procedure (intellisence can’t find it, neither can the ‘code’) so it assumes it’s an undefined variable.

I have literally placed my simplified version code in the two files, and I get the error.

Thanks -

It is a problem with declaring variables! Trust me, I do this for a living every long and boring day except when I am surfing straightdope

You made a class called tool. Tool is now like integer, decimal, or string in the sense that you need to set it equal to a variable (declare it) in order to use it. As long as you have Option Explicit On (if you don’t you really should!) you can’t just say:

strThis = “hello”
without:
dim strThis as string

Same goes for class tool, like Armilla says try:

Dim objTool as Tool
Set objTool = New Tool

Good luck!

Yep, yep you are right. I got it working. Thanks. Christ this is the first time i’ve really delved into classes and the application I am writing this for is the biggest thing to every use the ‘COM’ model (or so I’ve been told).

The object model diagram is made up of 38 40"x30" posters size prints.

(I swear I had already replied to you earlier. Sorry I took so long)

Thanks everyone.