VB.NET Module - Can I force the use of <Module_Name>.Public_Member_Name when accessing pub. Members?
I have a situation where I have several VB.NET Modules in the same Logic开发者_开发问答al-Module of a large application.
I would like the update function of each module to be public, but I would like users to be forced to qualify the function call with the module name.
ModuleName.Update()
instead of
Update()
Is this possible?
Thanks.
No.
The VB.NET specifications automatically use Type Promotion to allow this behavior to occur. The only way to avoid this is to have a type at the namespace that has the same name (Update) which would prevent (defeat) the type promotion provided in VB.NET.
Yes, it is possible, if you are willing to wrap the module within a namespace of the same name as the module:
Namespace ModuleName
Module ModuleName
...
End Module
End Namespace
Using modules is usually a poor design, because its methods become visible directly in the name space.
Consider replacing them with Classes. Put Shared
on all the members:
Class ClassName
Public Shared Property SomeData As Integer
Public Shared Sub Update()
End Sub
End Class
Update
would be referenced as:
ClassName.Update()
Make it impossible to instantiate, by having a Private instance constructor (is NOT Shared
):
Private Sub New()
End Sub
Any needed class instantiation can be done like this:
Shared Sub New()
... code that runs once - the first time any member of class is accessed ...
End Sub
精彩评论