2

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.

5
  • See this duplicate: stackoverflow.com/questions/881570/classes-vs-modules-in-vb-net Commented Apr 15, 2010 at 14:55
  • It didn't answer my question. I did see it though, thanks. Commented Apr 15, 2010 at 14:57
  • 1
    What's left of your question after reading that? Please be more specific, as your question sounds very much like that one. Commented 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! Commented Sep 16, 2011 at 9:02
  • Look at the following playlist on YouTube it explains classes and objects. youtube.com/playlist?list=PL3FEE93A664B3B2E7&feature=plcp Commented Feb 22, 2012 at 16:05

3 Answers 3

7

(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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

You can avoid having to type ClassName.SharedMethodName by using Imports ClassName then you can just type SharedMethodName
0

Module 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.