在VBA中将类模块用作名称空间是否有任何陷阱 [英] Are there any pitfalls in using class modules as namespaces in VBA

查看:59
本文介绍了在VBA中将类模块用作名称空间是否有任何陷阱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VBA不支持名称空间(Java poeple的软件包).其他语言的命名空间有助于避免歧义,并补充成员名称的自动补全等工具.

VBA does not support namespaces (packages for Java poeple). Namespaces in other languages help avoid ambiguity and supplement tools like autocompletion of member names.

在VBA中模拟名称空间的一种方法是在Class模块中声明事实上的静态方法,然后在标准模块中声明该类的默认实例.这甚至是某些Microsoft文档中遵循的模式.您也可以使用这种方法来嵌套伪命名空间.

One way to simulate namespaces in VBA is to declare what are defacto static methods in a Class module and then declare a default instance of that class in a standard module. This is even the pattern followed in some Microsoft documentation. You can use this approach to nest pseudo-namespaces as well.

您,我的同伴,使用这种方法会遇到任何技术问题吗?

Do you, my peers, anticipate any technical problems with using this approach?

这不是民意测验问题;我不是在问正统或美学吸引力.我问,这会引起问题吗?如果是这样,那又是什么?"

This is not a poll question; I'm not asking about the orthodoxy or aesthetic appeal. I'm asking, "will this cause problems? If so, what?"

示例:

' Class module called clsIOText
Public Function ReadAllText(ByVal FileName As String) As String
    With New FileSystemObject
        With .OpenTextFile(FileName, ForReading)
            ReadAllText = .ReadAll
            .Close
        End With
    End With
End Function

' Class module call clsIO
Public Text As New clsIOText

' In a standard module somewhere
Public IO As New clsIO

' Usage like a namespace
' 1. Fully qualified
Debug.Print IO.Text.ReadAllText("C:\temp\example.txt")

' 2. With a namespace alias-like object
Dim Text As clsIOText
Text = IO.Text
Debug.Print Text.ReadAllText("C:\temp\example.txt")

推荐答案

到目前为止没有其他答案...

No other answers so far...

我想出的陷阱是更多的限制和陷阱:

The pitfalls I've come up with myself are more limitations and gotchas:

  1. 您需要将每个类的Instancing属性设置为"2-PublicNotCreatable".否则,如果项目被编译后又被另一个项目引用,则成员将不会出现在对象浏览器中(尽管它们仍然可用)
  2. 您不能在类模块中将外部API声明声明为Public.为了将它们公开给类之外的代码,您必须将Declare设为私有,然后创建一个公共包装器方法.
  3. 这仍然不允许您组织名称空间中的类型-仅静态方法的集合.
  4. 从查询和宏中,您只能调用在标准模块中定义的全局方法.因此,要使用包装在名称空间类中的所有函数,您必须在标准模块中定义包装器,以简化层次结构.
  1. You need to set the Instancing property of each class to "2 - PublicNotCreatable". Otherwise if the project is compiled and then referenced from another project, the members will not appear in the Object Browser (though they will still be useable)
  2. You cannot declare external API declarations in a class module as Public. In order to expose them to code outside the class you must do the Declare as private and then make a public wrapper method.
  3. This still does not allow you to organize types in namespaces -- only collections of static methods.
  4. From queries and macros you can only call global methods defined in a standard module. So to use any functions wrapped up in namespace classes you'll have to define wrappers in a standard module, reflattening the heirarchy.

这篇关于在VBA中将类模块用作名称空间是否有任何陷阱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆