1

I have been new to Visual Basic. Why exactly do we use a module in VB.NET?

What would be a small example of a module and calling it one of the form?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3169288
  • 21
  • 1
  • 4
  • 1
    Short answer - don't use modules in VB.NET. Especially because you are new, don't get into bad habits. :) There is a good use though - Extensions. Other than that - use shared classes. – Victor Zakharov Jan 10 '14 at 19:06
  • Did you read the documentation? – SLaks Jan 10 '14 at 19:07
  • 3
    @Neolisk i disagree with that assertion. Modules have a purpose in VB.net and using them for that purpose is 100% acceptable. Certain features simply can't be used without them. A blanket assertion that they are bad is wrong. – JaredPar Jan 10 '14 at 19:07
  • 2
    @JaredPar: In my experience, they are more "bad" than "good". Which features beside Extensions do you mean? To elaborate, I've seen people doing complete chaos in the project, just because they think modules are okay to use. It's like "okay to carry a loaded gun with you at all times". – Victor Zakharov Jan 10 '14 at 19:08
  • @Neolisk extension methods, declarative non-OO code, project level members / data – JaredPar Jan 10 '14 at 19:10
  • 1
    Declarative non-OO code is what? For project level members and data, there are shared classes, even though doing it so has repercussions, and becomes a maintenance nightmare at a certain point. But using modules becomes a nightmare much sooner. C# devs somehow manage without modules, right? :) – Victor Zakharov Jan 10 '14 at 19:12
  • 1
    I wonder what transpired the decision to use static classes for writing extensions in C#, but keep them in modules in VB.NET. VB.NET could have also used shared classes, to keep consistent, then modules would become obsolete. If anybody knows the full story, please share your thoughts. – Victor Zakharov Jan 10 '14 at 19:18
  • 1
    @Neolisk i think your assertion that modules lead to chaos is completely unfounded. There is 0 difference between a class with all static members and a module. The primary difference is whether or not the members need a qualifier. – JaredPar Jan 10 '14 at 19:21
  • @JaredPar: This a huge difference, believe me. Anything global is bad in code. The more local it is, the easier it is to maintain, but I think we are getting off-topic with this. :) – Victor Zakharov Jan 10 '14 at 19:21
  • @Neolisk as I already said there no difference between modules and static classes. Your assertion that one is fine and the other is broken is simply incorrect. There is a small difference in name resolution only, it has no affect on global data – JaredPar Jan 10 '14 at 19:29

1 Answers1

6

A Module in VB.Net is essentially a grouping a methods and fields. These members can be accessed without qualification anywhere that the module itself is accessible. This mechanism is actually how many of the standard VB operators ChDir, ChDrive, CurDir are implemented. There is a module in the VB runtime named FileSystem which defines all of these operations

The main advantages / differences between Module and Class are the following

  • It's a declarative grouping of functions that aren't associated with instances of an object
  • It's the only way to define extension methods in VB.Net
  • No need for a redundant qualifier on every usage of a project helper method
  • No need to protect a module from accidental instantiation by the developer. It's by definition not creatable
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454