Zay Lau Zay Lau - 20 days ago 5
Vb.net Question

VB.Net module behavior

I am having a "weird" situation of my VB.Net modules, as per my understanding,

Module
in VB.Net means static class so I have implemented a couple of helper modules with couple of functions each, let's have some examples for better explanation (free hand code, may contains syntax problem):

Namespace Helpers
Module HelperA
Public Function FunctionA() As Boolean
Return True
End Function
End Module
End Namespace

Namespace Helpers
Module HelperB
Public Function FunctionB() As Integer
Return 1
End Function
End Module
End Namespace


When I start coding in Visual Studio and type
Helpers.
, both
FunctionA()
and
FunctionB()
are show up in the recommended auto-complete dialog which I have not type
HelperA
or
HelperB
yet, I have some C#.Net projects with static class and I found such behavior does not apply to C#.Net static class.

It is weird to me and inconvenience since I am now having 50-ish functions under a single namespace, have done some Google but nothing could be find, could anyone suggest a solution (besides change
Module
to
Class
) or any keywords to search with?

Any help will be appreciate!

Answer

Module doesn't technically mean static class. Static in VB.net (with regard to functions) is Shared, and there is no Shared Class. What I think you want is a sealed/abstract/not-inheritable class with static/shared functions (you'll be able to call the functions without an instance of the parent class, but you'll still have to reference the parent class when calling the function). If that's the case, then do something similar to the following:

Public NotInheritable Class HelperA
    Public Shared Function FunctionA() as Boolean
        Return True
    End Function
End Class

Having said that, the only difference I've found—at least for practical purposes—between a shared function and a module function is that module functions can be called without referencing the module.

Comments