VBA 不支持命名空间(Java poeple 的包)。其他语言的命名空间有助于避免歧义并补充工具,如成员名称的自动完成。
在 VBA 中模拟命名空间的一种方法是在 Class 模块中声明什么是事实上的静态方法,然后在标准模块中声明该类的默认实例。这甚至是 pattern followed in some Microsoft documentation 。您也可以使用这种方法来嵌套伪命名空间。
您,我的同行,是否预计使用这种方法会出现任何技术问题?
这不是民意调查问题;我不是在问正统观念或审美吸引力。我在问,“这会引起问题吗?如果是这样,那会怎样?”
例子:
' 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")
最佳答案
目前没有其他答案...
我自己想出的陷阱是更多的限制和陷阱:
Declare
设为私有(private),然后创建一个公共(public)包装器方法。