vba 跨各种模块管理用户定义的类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3977994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Managing user-defined types across various modules
提问by Jean-Fran?ois Corbett
What is the best way to manage common user-defined types across VBA modules?
跨 VBA 模块管理常见用户定义类型的最佳方法是什么?
I use the same user-defined types in different modules. For example, I often need to represent (x,y) points, so I end up having this Type
in different modules:
我在不同的模块中使用相同的用户定义类型。例如,我经常需要表示 (x,y) 点,所以我最终Type
在不同的模块中使用它:
Type XYpointType
x As Double
y As Double
End Type
I pass arguments of type XYpointType
to and from subs and functions in different modules.
我XYpointType
在不同模块中将类型参数传入和传出subs 和函数。
However, I suspect this is a poor way to manage user-defined types. The exact same Type
definition code ends up in many different modules.
但是,我怀疑这是管理用户定义类型的糟糕方法。完全相同的Type
定义代码最终出现在许多不同的模块中。
Alternatively, I could have this Type
declaration in a single, central "types" module, and all other modules needing this particular type should refer to the types module. The downside is that each module loses its "modularity" in that it must be accompanied byt the "types" module wherever it goes.
或者,我可以Type
在单个中央“类型”模块中进行此声明,并且所有其他需要此特定类型的模块都应引用 types 模块。缺点是每个模块都失去了它的“模块化”,因为它无论走到哪里都必须伴随着“类型”模块。
Any suggestions?
有什么建议?
采纳答案by jtolle
This is interesting, because I never knew that you could have two modules both declaring a Public Type with the same name. Now that I do know, I'm horrified.
这很有趣,因为我从来不知道可以有两个模块都声明一个具有相同名称的公共类型。现在我知道了,我很害怕。
So first, Eric Towers is correct that you want to put your Type declaration in only one module. Other modules that use this Type will depend on that module being available, but that's just something that comes with modularization. The need to manage dependencies between modules is common to all software development. It's unfortunate that within a single VBA project (like the kind you'd find in a single Excel workbook) the only option for managing module dependencies is manual. (In VBA, "modules" are really "source code files". Other languages/environments have various higher-level packaging systems.)
因此,首先,Eric Towers 是正确的,您只想将类型声明放在一个模块中。使用此类型的其他模块将取决于该模块是否可用,但这只是模块化带来的东西。管理模块之间的依赖关系是所有软件开发的共同需求。不幸的是,在单个 VBA 项目中(就像您在单个 Excel 工作簿中找到的那种),管理模块依赖项的唯一选择是手动。(在 VBA 中,“模块”实际上是“源代码文件”。其他语言/环境具有各种更高级别的打包系统。)
Now for the horrifying part. If you do declare Types with the same name in different modules, you're setting yourself up for problems. Consider two VBA Modules:
现在是可怕的部分。如果您确实在不同的模块中声明了具有相同名称的类型,那么您就会遇到问题。考虑两个 VBA 模块:
'Module1
Public Type typ
x As String
End Type
Public Sub useTyp()
Dim t As typ
t.x = 42
Debug.Print t.x + t.x
End Sub
and
和
'Module2
Public Type typ
x As Long
End Type
Public Sub useTyp()
Dim t As typ
t.x = 42
Debug.Print t.x + t.x
End Sub
in the same project, your code will "compile" (at least in Excel VBA). But the two 'useTyp' subs give different output. Worse, if you remove one of the Type declarations from one of the modules, you've suddenly changed the behavior of the 'useTyp' routine in the same module! This kind of ambiguity is never desirable.
在同一个项目中,您的代码将“编译”(至少在 Excel VBA 中)。但是两个“useTyp”子函数给出了不同的输出。更糟糕的是,如果您从模块之一中删除类型声明之一,您会突然改变同一模块中“useTyp”例程的行为!这种歧义是永远不可取的。
What's going on is that VBA is really creating two different types, 'Module1.typ', and 'Module2.typ'. When you're in the same module and don't qualify the type name, VBA silently finds the "right" type and uses it.
发生的事情是 VBA 真正创建了两种不同的类型,“Module1.typ”和“Module2.typ”。当您在同一个模块中并且不限定类型名称时,VBA 会默默地找到“正确”的类型并使用它。
I'm a little surprised to hear that you're passing around instances of one 'XYpointType' to modules that have a different 'XYpointType' declaration. It's not that important, but could you post the code? I'm interested in nitpicky things like this...
听到您将一个“XYpointType”的实例传递给具有不同“XYpointType”声明的模块,我有点惊讶。这不是那么重要,但是您可以发布代码吗?我对这种挑剔的东西很感兴趣......
回答by Eric Towers
Use your second method. The point is to make a reusable Types blob. To be reusable, it must be separated. Then, yes, every module that uses those types must reference that blob. But the same thing may be said about modules calling into each other, forms requiring the modules they call, et c.
使用您的第二种方法。重点是制作一个可重用的类型 blob。为了可重复使用,它必须分开。然后,是的,使用这些类型的每个模块都必须引用该 blob。但是对于相互调用的模块,需要它们调用的模块的表单等,也可以说同样的事情。
回答by Fink
You can also create a class for your XYPoints. This will allow you to have custom functions, and methods should you need to go down that road. Types are very limited compared to classes.
您还可以为 XYPoints 创建一个类。这将允许您拥有自定义函数和方法,如果您需要沿着这条路走下去。与类相比,类型非常有限。
Here is a good resource to get you started: http://www.cpearson.com/excel/Classes.aspx
这是一个很好的资源,可以帮助您入门:http: //www.cpearson.com/excel/Classes.aspx