What is the difference between a Module and a Class in VB.NET?
Quick Answer
A Module in VB.NET is like a C# static class — you can't instantiate it and all members are shared. But its members are also globally accessible within the namespace without qualifying with the module name. A Class is a normal type you instantiate.
A Module is VB.NET's version of a static class — no instances, everything is shared. The distinctive thing is that the members are visible globally within the namespace, so a function in Module Utilities can be called just by its name without Utilities. in front. That's convenient but it clutters the namespace. A Class is a regular type you create instances of. These days I'd lean toward a normal class with Shared methods that you have to qualify, because it's clearer where things come from.
- Module members:
Public Sub Helper()inModule Utilscan be called as justHelper()anywhere in the namespace — convenient but pollutes the global scope. - Historically used for the app entry point (
Sub Main) and shared utility functions. - Modern preference: a normal class with
Sharedmembers, called with the class name, for clarity.