Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 Logical-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.

like image 463
Brian Webster Avatar asked Oct 16 '25 21:10

Brian Webster


2 Answers

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
like image 91
Wolfgang Grinfeld Avatar answered Oct 19 '25 13:10

Wolfgang Grinfeld


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