472,958 Members | 2,643 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 11454
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...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.