views:

106

answers:

4

I am not a program designer by any means but I would really like to start getting a better grasp of how to do it and a better understanding of the .NET languages in general (VB, C#). I was reading a book by Wrox - Professional Visual Basic 2008. In it I believed it mentioned that Modules are slowly going out of existence. I can see why most coding would go into a class object but I would assume modules would always be necessary to at least keep the code clean.

Could anybody clarify this up for me? Also, I have been searching for a good source on software design but I can't seem to find any recent books published. I might be searching in the wrong places but I would really like to get my hands on one.

Thank you.

A: 

Modules are the closest thing VB has to static classes, which can be very useful, even when programming in an object-oriented environment.

And since VB has no static classes, modules are as far as I know the only way to create extension methods.

John M Gant
A: 

You need modules in order to define your own Extension methods

Damien_The_Unbeliever
Thank you for your response. I have never even heard of Extension methods but I see that they can ONLY be done in modules, and although this does answer my question, I was more concerned about the structure of programs and using modules.
AdamBeck
+2  A: 

While in general they don't quite fit with OOP, they are still used and are required in some cases.

In VB.Net, if you wish to write extension methods, you are going to have to use a Module - The compiler will only allow Extension Methods to be defined in one.

You could of course get round not using Modules - an Non Inheritable Class with a private constructor and nothing but Shared Methods will achieved the same thing as a Module.

Like everything in programming (and many other things), they have their uses, and as long as they are not miss-used there is no problem with them. Right tool for the job!

Pondidum
+3  A: 

The Module keyword in VB.NET primarily exists for compatibility with VB6 and earlier. Back then, most VB code was procedural with free-standing non-class Subs and Functions. The language acquired the Class keyword somewhere around VB4. Not true classes in the OOP sense, it didn't support inheritance. A feature missing from the underlying COM architecture.

It doesn't fit very well with the execution model provided by the CLR. There is no support for free functions, every method must be a member of a class. The VB.NET compiler emulates modules by declaring a class, the module procedures become Shared methods of that class. You can see this with Ildasm.exe:

.class private auto ansi sealed ConsoleApplication1.Module1
       extends [mscorlib]System.Object
{
  .custom instance void [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute::.ctor() = ( 01 00 00 00 ) 
} // end of class ConsoleApplication1.Module1

Note how it is private, so that code can't get a reference to it, and sealed, so that no code can derive a class from a module.

The C# compiler does the exact same thing with a "static class", the CLR doesn't have a notion of static classes either. There are plenty of good reasons for static classes, the idea of "Module" isn't obsolete. You could accomplish the same by declaring a NotInheritable Class in VB.NET code, having only Shared methods. The VB.NET compiler however doesn't enforce methods to be Shared like the C# compiler does and doesn't allow you to declare the class private. As such, a Module is just fine.

Hans Passant
+1 You give a very good explanation of Modules. I migrated from 3 years of VB6 and I've been using VB.NET for over 5 years but hadn't paid much attention to this detail. Thanks for the info.
Alex Essilfie