开发者

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

开发者 https://www.devze.com 2023-02-04 08:11 出处:网络
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

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 gett开发者_StackOverflow中文版ing it'?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号