473,386 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Shared classes or modules?

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
5 11552
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
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
* "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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rickard Lind | last post by:
Is there any way to build the python executable statically and still be able to load modules built as shared libraries? I'm trying to run python scripts on a stripped down FreeBSD (4.9) machine...
1
by: adamcrew | last post by:
Hi, I'm not sure if this is truly a C++ question, but it's something that has only come up now that I'm working in a C++ environment. Essetnially, what has happened is that I've come in to a new...
0
by: Shiraz | last post by:
Hi I have a question regarding the functionality of merge modules. Since this relates to my previous queries, I'll just give you a brief background on the topic. I had to make an installer for...
15
by: Rob Nicholson | last post by:
A consequence of the ASP.NET architecture on IIS has just hit home with a big thud. It's to do with shared variables. Consider a module like this: Public Module Functions Public GlobalName As...
4
by: Chris Ashley | last post by:
Is there any disadvantage to using a class with shared methods as opposed to a module? I suppose the end result is the same, but I'm curious about any other issues. Regards, Chris
14
by: Joe Fallon | last post by:
I am trying to build a Data Access Layer for a SQL Server back end. My class has a number of methods in it. The constructor takes a connection string. I currently have to instantiate an object...
6
by: Daniel Fernandes | last post by:
Hi there Is there any difference I need to be aware when I create a class with only shared members & procedures when compared to a module (which is a shared class) ? I am asking this because...
1
by: Mark Denardo | last post by:
Ok here's my problem: I have a bunch of Classes at the same namespace level say "abc.xyz". And all Classes reside in different files. abc.xyz.Class1 (in Class1.vb) abc.xyz.Class2 (in...
2
by: mgoold2002 | last post by:
Hello. I've just begun programming in VB .NET, and I'm trying to turn all my modules into classes. In order to retrieve/exchange values from one class to another, I initiated New instances of the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.