What advantage is there, if any, to using Modules rather than classes in VB? How do they differ, and what advantages/disadvantages are there in using modules? In VB or VB.NET, I use both.
-
See this duplicate: stackoverflow.com/questions/881570/classes-vs-modules-in-vb-netKamarey– Kamarey2010-04-15 14:55:33 +00:00Commented Apr 15, 2010 at 14:55
-
It didn't answer my question. I did see it though, thanks.Arlen Beiler– Arlen Beiler2010-04-15 14:57:35 +00:00Commented Apr 15, 2010 at 14:57
-
1What's left of your question after reading that? Please be more specific, as your question sounds very much like that one.John Saunders– John Saunders2010-04-15 17:31:48 +00:00Commented Apr 15, 2010 at 17:31
-
I also created Module and Class having the same output. It's okay to use class than module, it depends upon the tastes of every programmer. If you want to know my basic sample code, you can visit my site. Just click here! Hopefully, you appreciate it! Thanks.. I want to share this. If you like, comment me and also share what you have done about vb.net..Thanks again!user948461– user9484612011-09-16 09:02:53 +00:00Commented Sep 16, 2011 at 9:02
-
Look at the following playlist on YouTube it explains classes and objects. youtube.com/playlist?list=PL3FEE93A664B3B2E7&feature=plcpChester– Chester2012-02-22 16:05:12 +00:00Commented Feb 22, 2012 at 16:05
3 Answers
(A) Modules
and
(B) Classes with only Shared functions
solve the same problem: Both allow you to logically group a set of functions.
Advantages of using a module:
- It allows you to define extension methods.
- For someone reading your code, it is immediately obvious that this is not a class representing a group of stateful objects but just a "function container".
Advantages of using a class with shared functions:
- It's easy to extend it with instance (= non-shared) variables, functions and properties later on.
So, if you are writing a set of helper functions and want to logically group them (where the concept of a state of this group just doesn't make sense), use a module -- this is exactly what they are here for. On the other hand, if you have a function that conceptually fits to an already existing class, add it as a shared function to that class.
Comments
A major difference is that methods in modules can be called globally whereas methods in classes can't. So instead of ModuleName.MyMethod() you can just call MyMethod(). Whether that is an advantage or disadvantage depends on the circumstances.
1 Comment
ClassName.SharedMethodName by using Imports ClassName then you can just type SharedMethodNameModule are come earlier and now VB.NET just let it for backward compatibility. Modules and Class are nearly same. You can call Module.Function() directly as it treat as Shared Function in a class. Class you can define Shared Function/Method and additionally can create an instance like Dim c as Class = New Class().
Avoid use of Module, instead use Class. it is good for you to write a better OOP programming.