Martijn
Martijn

Reputation: 12102

When is it a good idea to use a vb.net Module

Some of my co-workers make extensive use of the VB.net concept of Modules. Unfortunately, I just don't 'get it'. I see no benefit in using modules over shared classes. Am I missing something? When would it be preferable to use a module? Or am I (as I do quite often in this language) 'just not getting it'?

Upvotes: 1

Views: 1680

Answers (2)

John
John

Reputation: 30586

In VB.net a module is a shared class. When they are compiled they are given a private constructor and methods set to shared.

There are some times when you are forced to use modules by the compiler (in the same way static classes are in C#) such as for extension methods which can not be created in side a VB.Net class.

By using modules for your helper methods you will make it easier to convert them over to extension methods later and restrict others from adding any instance methods or constructors.

That said they are a hang over from VB6 that did not support full OO programming and beyond standalone helper methods they would not widely be used.

Upvotes: 2

cHao
cHao

Reputation: 86575

A module is essentially the same as a shared class. The major difference is that in a module, there's no need for all the extra "shared"s, cause everything's implicitly shared. If you have no instance data and are just using the class as a kind of namespace for functions, then it's a better idea (IMO) to use a module instead and make that clear.

Upvotes: 1

Related Questions