469,623 Members | 1,971 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

Difference between Class and module

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

Jun 25 '06 #1
10 5149
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/micro...rch+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" <Bo****@hotmail.com> schreef in bericht
news:11**********************@p79g2000cwp.googlegr oups.com...
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

Jun 25 '06 #2
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]" <no************@planet.nl> schreef in bericht
news:OJ**************@TK2MSFTNGP02.phx.gbl... 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/micro...rch+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" <Bo****@hotmail.com> schreef in bericht
news:11**********************@p79g2000cwp.googlegr oups.com...
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


Jun 25 '06 #3
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:el**************@TK2MSFTNGP03.phx.gbl...
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]" <no************@planet.nl> schreef in bericht
news:OJ**************@TK2MSFTNGP02.phx.gbl...
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/micro...rch+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" <Bo****@hotmail.com> schreef in bericht
news:11**********************@p79g2000cwp.googlegr oups.com...
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



Jun 25 '06 #4

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

Jun 26 '06 #5
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" <la*******@hotmail.com> schreef in bericht
news:11**********************@c74g2000cwc.googlegr oups.com...
Michel Posseth [MCP] wrote:

--
Larry Lard
Replies to group please

Jun 26 '06 #6
I forgot the :-)
"Cor Ligthert [MVP]" <no************@planet.nl> schreef in bericht
news:O8**************@TK2MSFTNGP02.phx.gbl...
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" <la*******@hotmail.com> schreef in bericht
news:11**********************@c74g2000cwc.googlegr oups.com...

Michel Posseth [MCP] wrote:

--
Larry Lard
Replies to group please


Jun 26 '06 #7
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" <Bo****@hotmail.com> wrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
| 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
|
Jun 26 '06 #8
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]" <Ja************@tsbradley.net> schreef in
bericht news:e1*************@TK2MSFTNGP05.phx.gbl...
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" <Bo****@hotmail.com> wrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
| 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
|

Jun 26 '06 #9
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.StandardMod uleAttribute 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]" <no************@planet.nl> wrote in message
news:Or****************@TK2MSFTNGP04.phx.gbl...
| 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]" <Ja************@tsbradley.net> schreef in
| bericht news:e1*************@TK2MSFTNGP05.phx.gbl...
| > 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" <Bo****@hotmail.com> wrote in message
| > news:11**********************@p79g2000cwp.googlegr oups.com...
| > | 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
| > |
| >
| >
|
|
Jun 27 '06 #10
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]" <Ja************@tsbradley.net> schreef in
bericht news:%2****************@TK2MSFTNGP04.phx.gbl...
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.StandardMod uleAttribute 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]" <no************@planet.nl> wrote in message
news:Or****************@TK2MSFTNGP04.phx.gbl...
| 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]" <Ja************@tsbradley.net> schreef
in
| bericht news:e1*************@TK2MSFTNGP05.phx.gbl...
| > 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" <Bo****@hotmail.com> wrote in message
| > news:11**********************@p79g2000cwp.googlegr oups.com...
| > | 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
| > |
| >
| >
|
|

Jun 27 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Johnny Lee | last post: by
5 posts views Thread by sophie_newbie | last post: by
21 posts views Thread by Nikolaus Rath | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.