GivenRandy
asked on
Collections and UDTs
The code is making a collection of user-defined data types (UDTs), but gets this error:
>Compile error:
>
>Only user-defined types defined in public object modules
>can be coerced to or from a variant or passed to late-
>bound functions
The data type is declared Public in a code module. The collection additions work fine with simple types (e.g., "Hello, world." strings), but not with the UDT.
>Compile error:
>
>Only user-defined types defined in public object modules
>can be coerced to or from a variant or passed to late-
>bound functions
The data type is declared Public in a code module. The collection additions work fine with simple types (e.g., "Hello, world." strings), but not with the UDT.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yep! Thanks, bob_online. That, and the reasons bhess1 mention, are why we almost exclusively use classes. This wayfaring UDT got changed to a class. Dang UDTs.
bhess1, look for points for yourself as well. Thanks for the in-depth discussion.
bhess1, look for points for yourself as well. Thanks for the in-depth discussion.
"A Collection object stores each item in a Variant. Thus the list of things you can add to a Collection object is the same as the list of things that can be stored in a Variant. This include standard data types, objects, and arrays but not user-defined types."
This is not quite complete. It is in fact possible to store a UDT in a Variant if the UDT is defined as Public in a Public Object Module - which ties in with your error message. Now, what is a Public Object Module?
Class modules in a regular VB EXE are object modules, but they are always private, i.e. not accessible from other projects.
A class module in an ActiveX DLL or ActiveX EXE will be a public object module as long as its Instancing property is not set to Private. So, for example, if you define your UDT as Public in a MultiUse class of an ActiveX DLL, and reference this DLL in your main application, you may then add UDTs to your collection.
You can thus create a collection of UDTs if you add an
ActiveX DLL project just for the purpose of defining the UDT. I'm not saying that this is worth the trouble (although it is very easy to do), but it can be done.
One caveat, however. If your UDT contains fixed-length strings you won't be able to define it this way, so back to sqaure one.
Because of all this hassle with UDTs it is usually better to use a class rather than a UDT and have a collection of class objects - perhaps as a collection class.