VBA 不支持名称空间(Java 人员的包)。其他语言的命名空间有助于避免歧义和补充工具,如成员名称的自动完成。
在 VBA 中模拟命名空间的一种方法是在 Class 模块中声明事实上的静态方法,然后在标准模块中声明该类的默认实例。这甚至是一些 Microsoft 文档中遵循的模式。您也可以使用这种方法来嵌套伪命名空间。
我的同行们,您是否预计使用这种方法会出现任何技术问题?
这不是民意调查问题;我不是在问正统或美学吸引力。我在问,“这会引起问题吗?如果会,会怎样?”
例子:
' 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")