By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,771 Members | 1,741 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,771 IT Pros & Developers. It's quick & easy.

Shared classes or modules?

P: n/a
Hello!

I have read some threads discussing the fact that a module is in reality a
shared class. If I try to create a Public Shared Class in vb.net I receive a
compile error. Why? If I can't explicitly create a shared class, how does
vb.net creates it?

TIA,
Erik Cruz
Nov 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Just a class whose members are all shared (all methods, properties, fields
etc, are shared).

-Rob Teixeira [MVP]

"Erik Cruz" <er******************@antares.com.br> wrote in message
news:eO**************@tk2msftngp13.phx.gbl...
Hello!

I have read some threads discussing the fact that a module is in reality a
shared class. If I try to create a Public Shared Class in vb.net I receive a compile error. Why? If I can't explicitly create a shared class, how does
vb.net creates it?

TIA,
Erik Cruz

Nov 20 '05 #2

P: n/a
Erik,
In addition to Rob's comments about a "Shared" class is a class that only
has Shared members (all methods, properties, fields etc, are shared).

Also you should make the constructor private, to prevent instantiating an
instance of the class and make the Class Notinheritable to prevent deriving
from the class.

A Module actually does not have a constructor, so you are preventing from
instantiating it. And a Module is Notinheritable to prevent deriving from
it.

I normally prefer "Shared" classes over Modules as they require the class
name to prefix the member names, which to me is better encapsulation (you
know where that identifier is coming from). However "Modules" are useful for
truly "global" functions, such as Math function.

You can use Imports on a class name so the class name is not required on
shared members. Such as:

Imports System.Math

Hope this helps
Jay

"Erik Cruz" <er******************@antares.com.br> wrote in message
news:eO**************@tk2msftngp13.phx.gbl...
Hello!

I have read some threads discussing the fact that a module is in reality a
shared class. If I try to create a Public Shared Class in vb.net I receive a compile error. Why? If I can't explicitly create a shared class, how does
vb.net creates it?

TIA,
Erik Cruz

Nov 20 '05 #3

P: n/a
* "Erik Cruz" <er******************@antares.com.br> scripsit:
I have read some threads discussing the fact that a module is in reality a
shared class. If I try to create a Public Shared Class in vb.net I receive a
compile error. Why? If I can't explicitly create a shared class, how does
vb.net creates it?


You will have to mark all methods as 'Shared'.

Notice that a module will be imported automatically, a C# shared class
won't.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #4

P: n/a
Hi Jay.

Now I understand it better, thanks. I have another question if you don't
mind. Other posts say that a module have no default constructor but it can
have a shared one if needed. If I declare a Public Shared Sub New() in my
module I get an error. How can a shared constructor be created on a module?
When is it useful?

Thanks again for your time.

Erik
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eV**************@TK2MSFTNGP11.phx.gbl...
Erik,
In addition to Rob's comments about a "Shared" class is a class that only
has Shared members (all methods, properties, fields etc, are shared).

Also you should make the constructor private, to prevent instantiating an
instance of the class and make the Class Notinheritable to prevent deriving from the class.

A Module actually does not have a constructor, so you are preventing from
instantiating it. And a Module is Notinheritable to prevent deriving from
it.

I normally prefer "Shared" classes over Modules as they require the class
name to prefix the member names, which to me is better encapsulation (you
know where that identifier is coming from). However "Modules" are useful for truly "global" functions, such as Math function.

You can use Imports on a class name so the class name is not required on
shared members. Such as:

Imports System.Math

Hope this helps
Jay

"Erik Cruz" <er******************@antares.com.br> wrote in message
news:eO**************@tk2msftngp13.phx.gbl...
Hello!

I have read some threads discussing the fact that a module is in reality a shared class. If I try to create a Public Shared Class in vb.net I receive
a
compile error. Why? If I can't explicitly create a shared class, how

does vb.net creates it?

TIA,
Erik Cruz


Nov 20 '05 #5

P: n/a
Erik,
Remember that in a Module all members are Shared implicitly, so for a module
you do not specify Shared.

Public Module Module1

Sub New()
End Sub

End Module

Is the same as:

Public Class Class1

Public Shared Sub New()
End Sub

End Class

The biggest advantage of a constructor in a Module is to contain
initialization code that is executed the very first time you use any member
of the Module. Which enables Lazy Initialization with little or no code from
you.

For example, if I had a LogModule that had methods to write items to a log
file. I could have the Sub New above open the log file the first time I went
to write to the file. When using methods of the module subsequently the log
file itself would already be open.

Imports

Public Module LogModule

Private m_stream As StreamWriter

Sub New()
m_stream = New StreamWriter("mylog.log")
End Sub

Public Sub Write(ByVal text As String)
m_stream.Write(text)
End Sub

End Module

The above also shows why I tend to prefer "Shared" Classes over Modules, the
Write method is ambiguous (yes I could have called it WriteLog, however
WriteLog is not as encapsulated...)

Hope this helps
Jay

"Erik Cruz" <er******************@antares.com.br> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi Jay.

Now I understand it better, thanks. I have another question if you don't
mind. Other posts say that a module have no default constructor but it can
have a shared one if needed. If I declare a Public Shared Sub New() in my
module I get an error. How can a shared constructor be created on a module? When is it useful?

Thanks again for your time.

Erik
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eV**************@TK2MSFTNGP11.phx.gbl...
Erik,
In addition to Rob's comments about a "Shared" class is a class that only
has Shared members (all methods, properties, fields etc, are shared).

Also you should make the constructor private, to prevent instantiating an instance of the class and make the Class Notinheritable to prevent deriving
from the class.

A Module actually does not have a constructor, so you are preventing from instantiating it. And a Module is Notinheritable to prevent deriving from it.

I normally prefer "Shared" classes over Modules as they require the class name to prefix the member names, which to me is better encapsulation (you know where that identifier is coming from). However "Modules" are useful

for
truly "global" functions, such as Math function.

You can use Imports on a class name so the class name is not required on
shared members. Such as:

Imports System.Math

Hope this helps
Jay

"Erik Cruz" <er******************@antares.com.br> wrote in message
news:eO**************@tk2msftngp13.phx.gbl...
Hello!

I have read some threads discussing the fact that a module is in

reality a shared class. If I try to create a Public Shared Class in vb.net I receive
a
compile error. Why? If I can't explicitly create a shared class, how

does vb.net creates it?

TIA,
Erik Cruz



Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.