views:

316

answers:

2

So I've been programming in C# for the last 6 years or so and now I'm getting my feet wet with VB.net.

The code base I'm working with uses some modules. To me the module looks a lot like a singleton. Only one exists; it can be called anywhere inside the namespace.

Is there something I'm missing here? Does VB not support the normal way a singleton is structured (private constructor/public instance field)?

+6  A: 

Modules are not a singleton. It is much more akin to a static class in C#. If you decompile the code you will see they have a very similar structure (modules have an extra attribute).

The major differences between a C# static class and a VB.Net module are ...

  • Don't have to add Static / Shared qualifiers to methods in a module. They are Shared by default and you cannot change this
  • If a Module is in an Imported namespace, all of its methods are available without qualification.
  • Static classes in C# can be generic, modules cannot (although they can have generic members)
JaredPar
So basically with a module it's like going through and declaring everyting inside static (which would be required in C#). Then the difference between that and a singleton is that a singleton supports inheritance (as a derived class, or from a interface) and polymorphism, where the module just is what it is? I'm just tring to sort it all out.
Joel Barsotti
@Joel, kind of. The important thing to remember about module is there is no instance ever. Under the hood it compiles down to a set of static methods (just like C# static classes). The biggest difference is they get added to the global name scope if the containing module is available.
JaredPar
+2  A: 

If I'm not mistaken, a VB module is the same thing as a static class.

sideproject
There are subtle differences between the 2 but yes at a high level they are the same.
JaredPar