Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot define a Public user-defined type within a private object mod

Status
Not open for further replies.

bashless

Technical User
May 3, 2002
35
US
Question:

VB6, Enterprise, standard exe, when I try to define a public user defined type in the general declarations area I get a compile error stating:
Cannot define a Public user-defined type within a private object module

I can define the type within a module and there is no problem there. Can you change the first form so that it is a public module or is it just hard coded that way?

Maybe there is a reason that I need to learn more about why it was done this way?

THanks..!
 
This problem is covered in the Help notes under
"Scope of Variables".
Basically, when you define a "local variable", you use the "Dim" keyword. When you define a user type, you use a module (an object-less form) and must define it using the "Public" keyword (so it can be used by any other form).

Those are just the rules of the road. You shouldn't
want to (or need to) break them.
 
The error you jst encountered tells you that UDTs can't be declared to private object modules, like your form. Even if you were able to declare your UDT in a standard module, you can't use it as a variable type for any procedure parameter.

And certainly you can't change your Form into a public object module. A Public Class module (ActiveX projects) are the public object module that can accept UDTs in their general declarations area.

I hope this helps!
 
Remember that when you create a udt you are creating your own variable type. This variable type has to be defined so that VB knows how to build a variable of that type. One reason that you must define the udt in a module is that when VB starts all modules are loaded automatically. Therefore the definition of your udt is available for creating a variable of that type.

If the udt were defined in a form1 then the possibility exists that a variable of that type is being created by another form (form2) and VB can't find the definition because form1 is not loaded yet. This will cause errors.

Forcing us to define udt's in modules is just a way of making sure that we avoid that potential error and we all just have to live with it. Thanks and Good Luck!

zemp
 
Thanks...I vote for zemp's response. It certainly makes sense.

Bashless
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top