Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Difference between Class and module

3 views
Skip to first unread message

Bonzol

unread,
Jun 24, 2006, 11:28:17 PM6/24/06
to
vb.net

Hey there, could someone just tell me what the differnce is between
classes and modules and when each one would be used compared to the
other?

Any help would be great

Thanx in advance

Cor Ligthert [MVP]

unread,
Jun 25, 2006, 1:19:47 AM6/25/06
to
Bonzol,

One of the thousand times asked question in this newsgroup.

A module is a simple format of a Class with only Shared members.

There is never a reason to use a module execpt if you have that already in
your VB6 converted program.

If you want to know more try a search on that in Google Newsgroups.

http://groups.google.com/group/microsoft.public.dotnet.languages.vb/search?group=microsoft.public.dotnet.languages.vb&q=class+module&qt_g=1&searchnow=Search+this+group

However feel free to ask this kind of questions again. You could not have
knowed that it is so often asked.

I hope that this gives an idea

Cor

"Bonzol" <Bon...@hotmail.com> schreef in bericht
news:1151206097.1...@p79g2000cwp.googlegroups.com...

Michel Posseth [MCP]

unread,
Jun 25, 2006, 12:19:00 PM6/25/06
to
Hmmm ,,, :-|

> There is never a reason to use a module execpt if you have that already in
> your VB6 converted program.

That is just your opinion Cor

To answer your question Bonzol

What is a module in VB.Net ?

A Module in VB.Net = a Class where Shared is implicitly understood for
each member. And the module name doesn't need to be supplied when the
members are used.

A Module compiles down to a NotInheritable class, with an attribute called
StandardModuleAttribute attached. As far as the compiled code goes, there's
no difference.

However here is also the catch why people in the full OOP camp say you
shouldn`t use modules
a NotInheritable(sealed) class which only consists of Shared (static)
methods is not very good object-oriented design. :-)

In my opinion there are two situations in wich using a Module might be valid

1. You do not care about OOP rules and just want to write as fast as
possible and as easy as possible a program in a situation were it is in your
opinion valid to use a module ( it's a style thingy ) i personally wrap
everything in a class when it can be represented as an object
( a module can save you a lot of typing )

2. when you want to override the standard method call behavior of VB.net

like a better IIF

Public Function IIf(Of T)(ByVal expression As Boolean, _
ByVal truePart As T, ByVal falsePart As T) As T

If expression Then

Return truePart

Else

Return falsePart

End If

End Function

This in a module will make anny call to iif in your code be redirected to
this "better" iif method so instead of having to change all your code you
can take advantage of language enhancements in the future by just writing
better methods as the language features ( in previous versions we did not
have generics , so in this situation it is a nice feature to have )

regards

Michel Posseth [MCP]

"Cor Ligthert [MVP]" <notmyfi...@planet.nl> schreef in bericht
news:OJ2o7aBm...@TK2MSFTNGP02.phx.gbl...

Cor Ligthert [MVP]

unread,
Jun 25, 2006, 1:12:48 PM6/25/06
to
Michel,

You have exactly written why I have that opinion, I could not have described
it better.

:-)

Cor

"Michel Posseth [MCP]" <MS...@posseth.com> schreef in bericht
news:elTm6LHm...@TK2MSFTNGP03.phx.gbl...

Larry Lard

unread,
Jun 26, 2006, 5:44:24 AM6/26/06
to

Michel Posseth [MCP] wrote:
> However here is also the catch why people in the full OOP camp say you
> shouldn`t use modules
> a NotInheritable(sealed) class which only consists of Shared (static)
> methods is not very good object-oriented design. :-)

To such people I like to say, Go tell that to System.Math :)

--
Larry Lard
Replies to group please

Cor Ligthert [MVP]

unread,
Jun 26, 2006, 6:14:18 AM6/26/06
to
Larry,

>> However here is also the catch why people in the full OOP camp say you
>> shouldn`t use modules
>> a NotInheritable(sealed) class which only consists of Shared (static)
>> methods is not very good object-oriented design. :-)
>
> To such people I like to say, Go tell that to System.Math :)

Why are people not allowed by you to give their opinion?

What is wrong with trying to do things as good as possible.

That their are still modules means that they have their pupose, but does not
give people like you the right to tell that they should be used.

Just my thought,

Cor


"Larry Lard" <larr...@hotmail.com> schreef in bericht
news:1151315064.7...@c74g2000cwc.googlegroups.com...

Cor Ligthert [MVP]

unread,
Jun 26, 2006, 6:54:00 AM6/26/06
to
I forgot the :-)


"Cor Ligthert [MVP]" <notmyfi...@planet.nl> schreef in bericht

news:O8PlLkQm...@TK2MSFTNGP02.phx.gbl...

Jay B. Harlow [MVP - Outlook]

unread,
Jun 26, 2006, 8:01:51 AM6/26/06
to
Bonzol,
In addition to the other Comments.

A Module is implicitly imported into every source file (effectively at the
project level). This cannot be turned off.

A Class with Shared methods (such as System.Math) needs to be explicitly
imported into every source file or at the project level.

Importing a class with shared methods allows to control if you want the
members qualified or not. For example:

Imports System.Math

Public Module MainModule

Public Sub Main()
Dim d As Double = Sin(90)
End Sub

End Module

verses:

Public Module MainModule

Public Sub Main()
Dim d As Double = Math.Sin(90)
End Sub

End Module


NOTE: When defining a class with only shared methods. I normally make it
Notinheritable with a private constructor. This prevents other developers
from creating an instance of the class and prevents inheriting from the
class.

Public NotInheritable Class Math

Private Sub New
End Sub

Public Shared Function Sin(value As Double) As Double
...
End Function

End Class

A Module (or static class in C# 2.0) simply doesn't have the constructor.
CLI/C++

I find both classes with shared method & Modules beneficial in a fully OOP
program. For example truly "global" functions such as System.Math or my
Generic IIF don't really fit in an OO world, and lend themselves well to
Modules... Normally I put utility/helper methods in a class with shared
methods, for example methods that need to be used from both Pages & Controls
in ASP.NET while instantiating a class doesn't really bring any value to the
solution...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


"Bonzol" <Bon...@hotmail.com> wrote in message
news:1151206097.1...@p79g2000cwp.googlegroups.com...

Cor Ligthert [MVP]

unread,
Jun 26, 2006, 9:18:55 AM6/26/06
to
Jay,

Basically I don't agree with you in this because that written modules as I
have seen them are mostly used as an unordered bunch of fields. I hate that,
but before answering you I took this time the effort by trying some things.

That made that I came by this.

Public Module GiveMySin
Public Function MyFunction() As Double
Return Sin(90)
End Function
End Module
Public Class GiveMyCos
Public Shared Function MyFunction() As Double
Return Cos(90)
End Function
End Class

I can call those by

Dim a As Double = GiveMySin.MyFunction
Dim b As Double = GiveMyCos.MyFunction

I can use in both intellisence. Used as this however, does the module
protect me from forgetting to write the shared key word or in other words
don't make it even necessary to write that..

Therefore I am in doubt now if the module used in this format is not a
better choice than the Shared Class.

Or do you see that I miss something?

:-)

Cor


"Jay B. Harlow [MVP - Outlook]" <Jay_Har...@tsbradley.net> schreef in
bericht news:e1OJRhR...@TK2MSFTNGP05.phx.gbl...

Jay B. Harlow [MVP - Outlook]

unread,
Jun 27, 2006, 1:53:49 AM6/27/06
to
Cor,

| Therefore I am in doubt now if the module used in this format is not a
| better choice than the Shared Class.
I agree if you "make a point" to qualify the module member's name I would
stay they are equal.

Unfortunately the IDE does not require you to qualify the module member's
name, nor does it require you to import the module name. This ease of
unqualified access of a Module's members is why I consider a shared class is
"better".

Yes you need take extra steps to define the shared class. However generally
you would define the class once, then use it a plethora of times. Its the
use a plethora of times where the benefit of the shared class over a module
is. When you prevent instantiating & inheriting a shared class, the compiler
will tell you if you forget to share one of its members...

Ideally I would prefer that VB introduce the equivalent of C#'s "static
class", a class that requires all shared methods and has no constructor.
Basically a Module that does not include the
Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute in the
generated IL. As I understand that the StandardModuleAttribute is what is
causing the implicit import.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


"Cor Ligthert [MVP]" <notmyfi...@planet.nl> wrote in message
news:Orz%23VLSmG...@TK2MSFTNGP04.phx.gbl...

Cor Ligthert [MVP]

unread,
Jun 27, 2006, 2:20:40 AM6/27/06
to
Jay,

I agree, but than are we talking about the module as I showed, which has
protection that it is not used in the not decided ways as you told. (If you
don't understand what I mean by that reply)

In fact do I find the names Shared or Static Class bad names, because it is
in fact for me a module. (Not in VB or C terminology). Where I find Shared
better because the use is currently absolute not Static anymore.

With that not meaning that it in my eyes the name should be a module.

Cor

"Jay B. Harlow [MVP - Outlook]" <Jay_Har...@tsbradley.net> schreef in

bericht news:%23sPQV4a...@TK2MSFTNGP04.phx.gbl...

0 new messages