Managing custom types of various modules

What is the best way to manage common user types through VBA modules?

I use the same custom types in different modules. For example, I often have to represent (x, y) points, so I get this Type in different modules:

 Type XYpointType x As Double y As Double End Type 

I pass arguments of type XYpointType to and from subsets and functions in different modules.

However, I suspect this is a poor way to manage user-defined types. Exactly the same Type definition code ends in many different modules.

Alternatively, I could have this Type declaration in one central “types” module, and all other modules that need this particular type should belong to the type module. The disadvantage is that each module loses its “modularity” in that it must be accompanied by a “types” module, wherever it is.

Any suggestions?

+4
source share
3 answers

This is interesting because I never knew that you could have two modules declaring a public type with the same name. Now that I know, I'm terrified.

So Eric Towers is correct that you want to place a Type declaration in only one module. Other modules that use this type will depend on whether this module is available, but this is just something that comes with modulation. The need to manage dependencies between modules is common to all software development. Unfortunately, in a single VBA project (such as you'll find in one Excel workbook), the only option for managing module dependencies is a guide. (In VBA, “modules” are indeed “source code files.” Other languages ​​/ environments have different higher-level packaging systems.)

Now for the horrific part. If you declare types with the same name in different modules, you are setting yourself up for problems. Consider two VBA modules:

 'Module1 Public Type typ x As String End Type Public Sub useTyp() Dim t As typ tx = 42 Debug.Print tx + tx End Sub 

and

 'Module2 Public Type typ x As Long End Type Public Sub useTyp() Dim t As typ tx = 42 Debug.Print tx + tx End Sub 

in the same project, your code will "compile" (at least in Excel VBA). But the two useTyp subsystems give different results. Even worse, if you delete one of the type declarations from one of the modules, you suddenly change the behavior of the "useTyp" routine in the same module! Such ambiguity is never desirable.

What happens, VBA does create two different types: Module1.typ and Module2.typ. When you are in the same module and do not qualify the type name, VBA silently finds the "correct" type and uses it.

I'm a little surprised to hear that you pass instances of one "XYpointType" to modules that have another "XYpointType" declaration. It's not that important, but can you post the code? I am interested in such ridiculous things ...

+3
source

Use the second method. The point is to make reusable blob types. To be reused, it must be separated. Then yes, every module that uses these types must reference this blob. But the same thing can be said about calls of modules to each other, forms requiring modules called by them, etc.

+3
source

You can also create a class for your XYPoints. This will allow you to have custom functions and methods that you will need to go down this road. Types are very limited compared to classes.

Here is a good resource to get you started: http://www.cpearson.com/excel/Classes.aspx

+1
source

All Articles