473,785 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11614
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******** ******@tk2msftn gp13.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******** ******@tk2msftn gp13.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******** ******@TK2MSFTN GP11.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******** ******@tk2msftn gp13.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("m ylog.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******** ********@TK2MSF TNGP11.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******** ******@TK2MSFTN GP11.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******** ******@tk2msftn gp13.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
2609
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 which has no shared system libraries so I want to link it statically against libc et al, but it would be nice to still be able to load modules which were built as shared libraries. In particular I have a library for which I've generated a wrapper...
1
1759
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 job where there has been no form of source control and been asked to co-ordinate the implementation of one. I was nice and comforable with the task until I found out about a practice that wasn't used in my previous C programming job.
0
2753
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 an application which was to share dlls with another application and these dlls were not self-registering. Someone on the forum suggested a few methods for the Visual studio installer, but since I had been using another software, a certain...
15
4944
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 String ' this is ineffect a global application object End Module
4
4369
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
6312
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 in order to use one of my methods. So I end up doing this quite a lot in my app. If I change all of the methods to be Shared does that mean I can just use them without instantiaing an instance of my DAL class?
6
1780
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 I have seen at work a class that declares a Database connection as a shared member and I wonder if that's a very bad programming practice..
1
1168
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 Class2.vb) abc.xyz.Class3 (in Class3.vb) .... abc.xyz.Class99 (in Class99.vb)
2
1468
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 classes in each class where I needed to retrieve a property or method. I discovered that this reciprocal loading of class instances led to stackoverflow exceptions. I am now remedying this problem by sharing properties among the classes. I...
0
9645
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
10147
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
10090
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
8971
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
7499
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
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.