VB.NET Module - Can I force the use of .Pu

2019-01-29 04:12发布

问题:

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.

回答1:

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.



回答2:

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


回答3:

Using modules is usually a poor design. Consider replacing them with Classes. A module in VB.NET is simply a static class. In other words it doesn't have to be instanced and you can call module.function. When you replace your modules with classes you will need to instance them. Then you can call class_instance.update with ease.