473,747 Members | 2,260 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5492
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.goo glegroups.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
StandardModuleA ttribute 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******** ******@TK2MSFTN GP02.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.goo glegroups.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.c om> schreef in bericht
news:el******** ******@TK2MSFTN GP03.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 StandardModuleA ttribute 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******** ******@TK2MSFTN GP02.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.goo glegroups.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*******@hotm ail.com> schreef in bericht
news:11******** **************@ c74g2000cwc.goo glegroups.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******** ******@TK2MSFTN GP02.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*******@hotm ail.com> schreef in bericht
news:11******** **************@ c74g2000cwc.goo glegroups.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.goo glegroups.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.MyFun ction
Dim b As Double = GiveMyCos.MyFun ction

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******** *****@TK2MSFTNG P05.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.goo glegroups.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.Visua lBasic.Compiler Services.Standa rdModuleAttribu te in the
generated IL. As I understand that the StandardModuleA ttribute 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******** ********@TK2MSF TNGP04.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.MyFun ction
| Dim b As Double = GiveMyCos.MyFun ction
|
| 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******** *****@TK2MSFTNG P05.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.goo glegroups.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
1876
by: jlopes | last post by:
I'm looking at the differences between these to forms and see no difference in their use. When accessed through a derived class. class ABase { public: virtual void filter(){ /* some code */ } }; class D_of_ABase : public ABase
10
2846
by: Johnny Lee | last post by:
Hi, I'm new in python and I was wondering what's the difference between the two code section below: (I) class TestResult: _pass_ = "pass" _fail_ = "fail" _exception_ = "exception"
5
7724
by: sophie_newbie | last post by:
OK this might seem like a retarded question, but what is the difference between a library and a module? If I do: import string am I importing a module or a library? And if i do string.replace() am I using a module or a function or a
4
1326
by: Radu | last post by:
Hi. I have the following question: Since the methods are virtual, the following code produces "Child other stuff" - the call runs as if pasted in Child... it runs in Parent but in the context of Child. ------------------------------- Module Module1 Sub Main() Dim obj As New Child()
13
3356
by: dmh2000 | last post by:
I am experimenting with the interactive interpreter environments of Python and Ruby and I ran into what seems to be a fundamental difference. However I may be doing something wrong in Python. Please comment and correct me if I am wrong In both languages, you can start up the interactive interpreter ('python' and 'irb'), load source files and do stuff, like create objects and call their methods. When you want to change something, you can...
23
14055
by: thebjorn | last post by:
For the purpose of finding someone's age I was looking for a way to find how the difference in years between two dates, so I could do something like: age = (date.today() - born).year but that didn't work (the timedelta class doesn't have a year accessor). I looked in the docs and the cookbook, but I couldn't find anything, so
0
2062
by: Shehryar | last post by:
AOA, I urgently want to know the major difference between "Module" and a "Class Module". Plz, reply me as early as possible. Thanx!
6
4031
by: JonathanOrlev | last post by:
Hello everyone, I have a newbe question: In Access (2003) VBA, what is the difference between a Module and a Class Module in the VBA development environment? If I remember correctly, new types of objects (classes) can only be defined in Class modules.
21
5195
by: Nikolaus Rath | last post by:
Hello, Can someone explain to me the difference between a type and a class? After reading http://www.cafepy.com/article/python_types_and_objects/ it seems to me that classes and types are actually the same thing: - both are instances of a metaclass, and the same metaclass ('type') can instantiate both classes and types. - both can be instantiated and yield an "ordinary" object - I can even inherit from a type and get a class
0
8979
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8818
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9522
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9355
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9307
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9225
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8234
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6790
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4860
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.