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

Shared Methods in a Class

P: n/a
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?

If so, does that mean the constructor does not run?
If so, how will my class methods know the ConnectionString value???

Can someone please outline the best way to resolve this?
Thanks!

--
Joe Fallon

Nov 20 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
On Sat, 4 Oct 2003 01:57:50 -0400, "Joe Fallon"
<jf******@nospamtwcny.rr.com> wrote:
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?
Yes.

If so, does that mean the constructor does not run?
Yes - but you can force code to run before any method is called (if
that's the behaviour you're after).
If so, how will my class methods know the ConnectionString value???
Is there a default connection string? If so, put it in a shared
property of your class and use the property everywhere else. If there
is no default connection string, you can work along another model:

1) Force the developer to instantiate a singleton (which accepts the
connection string as a parameter).
2) The singleton stores its instance (Me) in a shared property.
3) DAL makes use of the shared property.

i.e.

Public Class MyDatabase
Private Shared mInstance As MyDatabase = Nothing
Private mConnectionString As String

Private Sub New(oConnectionString As String)
mConnectionString = oConnectionString
End Sub

' This method is used by the BLL

Public Shared Function GetInstance(oConnectionString As String) _
As MyDatabase
If mInstance Is Nothing Then
mInstance = New MyDatabase( oConnectionString )
End Function
Return mInstance
End Function

' This method is used by the DAL

Friend Shared Function GetCurrentInstance() As MyDatabase
Return mInstance
End Function
End Class
The BLL developer would then have a single global, something like:

Public gDatabase As MyDatabase = MyDatabase.GetInstance(
Config.ConnectionString )

Can someone please outline the best way to resolve this?
Thanks!


There are many ways to implement a DAL. I guess you're trying to do
the right thing in that you are trying to make the interface as simple
as possible, right?

The way I prefer is to have an object to represent a Schema. The
Schema object knows how to communicate with Factory objects. Factory
objects know how to create (execute SQL), update, delete & find
DatabaseObject objects. And a DatabaseObject has a private member
indicating the schema it is associated with (so it can provide default
Save() and Delete() methods, and communicate with other factories).
From this model, you can tell that the only driver-dependent parts are
in the Factory objects (SQL Server, Oracle, Access...). Implementing
IDatabaseObject, IFactory and ISchema interfaces allows you to swap
database type. You might also want to spend five minutes specialising
a DataView (which implements the associated IObjectCollection) to wrap
your DatabaseObject.

Rgds,

Nov 20 '05 #2

P: n/a
Hi Joe,

Shared members belong to the Class as well as to the instances of the
Class, so they are accessible without needing to create an instance.

You will still need to use the class name to refer to the methods. Classes
can have what is called a class constructor. This is the same as for an
instance except that, being for the class itself, it has the Shared keyword.
This Shared New() will be automatically called when the Class is first
accessed.

Public Class SharedClass
Sub Shared New
MsgBox ("Hey - Classy or what!!")
End Sub
Public Shared Sub DoSomething
End Sub
End Class

In use:
'Will call New if this is the first use of SharedClass
SharedClass.DoSomething

In VB.NET you can eliminate even the need to have a Shared Class by
putting the methods into a Module. A Module is syntactic sugar - behind the
scenes it is a Shared Class. Because of this you don't need the Shared
keyword. Nor do you need to use the methods by giving the Module name (unless
there is a clash with something else, eg another Module, that also defines
that name).

Public Module Stuff
Sub New
MsgBox ("Hey - New Stuff!!")
End Sub

Public Sub DoSomething
End Sub
End Module

In use:
DoSomething
Whether you use a Shared Class or a Module is up to you. You could pose it
as a Best Practice question and get half-a-dozen opinions. ;-)

Regards,
Fergus
Nov 20 '05 #3

P: n/a
"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message news:<Oz**************@TK2MSFTNGP10.phx.gbl>...
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.
That way your business processes are not really data independend.
Since you need to specify the connection string in the calls to your
DAL.

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.
You might want to use global members of your business classes that you
instantiate in their constructors.

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?
Correct.

If so, does that mean the constructor does not run?
I guess so, never tested it. But it would be logical if this is
correct indeed.
If so, how will my class methods know the ConnectionString value???


They won't. The best way would be if your data access layer (which is
responsible for the data in your application) would be the only layer
to know how to connect to the DB. That way you keep the
responsibilities clear, which is one of the objectives of multitier
development.

If you want to stick with passing it from your business/presentation
classes, you will have to pass it with every Shared call.
Good luck

Freek Versteijn

PS: a nice thing to look at, if you are a bit more experienced with
the multitier design, would be the DAC2 component at
www.codeproject.com/useritems/dac2.asp
Nov 20 '05 #4

P: n/a
Hi Freek,

|| > If so, does that mean the constructor does not run?
||
|| I guess so, never tested it. But it would be logical if this is
|| correct indeed.

Lol, There's also a logic argument for the opposite case. ;-)

Shared Classes and Modules can both have their own constructors. See my
other post in this thread.

Regards,
Fergus
Nov 20 '05 #5

P: n/a
Hi Freek,

|| > If so, does that mean the constructor does not run?
||
|| Yes ...

Shared Classes and Modules can both have their own constructors. See my
other post in this thread.

Regards,
Fergus
Nov 20 '05 #6

P: n/a
On Sat, 4 Oct 2003 12:56:20 +0100, "Fergus Cooney"
<fi******@tesco.net> wrote:
Hi Freek,

|| > If so, does that mean the constructor does not run?
||
|| Yes ...

Shared Classes and Modules can both have their own constructors. See my
other post in this thread.


What's a "Shared Class"?

Nov 20 '05 #7

P: n/a
On Sat, 04 Oct 2003 17:07:50 +0100, _Andy_ <wi******@nospamthanks.gov>
wrote:
On Sat, 4 Oct 2003 12:56:20 +0100, "Fergus Cooney"
<fi******@tesco.net> wrote:
Hi Freek,

|| > If so, does that mean the constructor does not run?
||
|| Yes ...

Shared Classes and Modules can both have their own constructors. See my
other post in this thread.


What's a "Shared Class"?


By that, I mean...

Public Class God
Shared Sub New()
MsgBox("Not yet")
End Sub

Public Shared Sub DoYouExist()
End Sub
End Class

What the hell does "Shared Sub New" actually mean? Constructing an
object that doesn't exist? Yet? Jeez... it's such a horrible
construct... ;)

Rgds,

Nov 20 '05 #8

P: n/a
Hi Andy (sorry about your name in the last reply)

'Shared' - I prefer the C# keyword which is 'static' - meaning that it's
there once and for all. Also in C# the name of the class is used rather than
New. Lots of things seem to make better sense from the C# perspective. I
recommend learning it. ;-)

Shared New means 'get your act together class, I need you'.

Consider the following class:
Public Class Heaven
Private Shared ohGod As New God
Private Shared Jeez As New SonOf (God)
Private Shared alAngels As New ArrayList
Private Shared alSouls As New ArrayList
Private Shared GatesOfHeaven = New Gates (Not Bill)

Shared Sub New
alAngels.Add (New Angel (Michael, Angels.Arch))
alAngels.Add (New Angel (Gabriel, Angels.Normal))
GatesOfHeaven.Open
End Sub

Private Sub New 'There can be only one.
End Sub

Public Shared Function GoToHeaven (oPerson)
If oPerson.GoodDeeds < Enough Then
Throw New MiserableSinnerException
End If
alSouls.Add (oPerson.Soul)
oPerson.Dispose
End Sub
End Class

God and the Angels have to exist somewhere, for although Heaven cannot be
instantiated, it has members - it occupies memory. Thus the class Heaven is
actually an object in that sense.

Heaven exists as a potential at the start of the application and will
remain thus until the very end unless someone dies and tries to GoToHeaven.
Then the class will be loaded, the member fields will be created and
initialised and the construction of Heaven will take place. Not until the
GatesOfHeaven are finally opened will the bounty of Heaven be available to the
Mortal Masses and the DearDeparted may be finally Judged.

Regards,
Fergus
Nov 20 '05 #9

P: n/a
On Sat, 4 Oct 2003 18:48:08 +0100, "Fergus Cooney"
<fi******@tesco.net> wrote:
God and the Angels have to exist somewhere, for although Heaven cannot be
instantiated, it has members - it occupies memory. Thus the class Heaven is
actually an object in that sense.

Heaven exists as a potential at the start of the application and will
remain thus until the very end unless someone dies and tries to GoToHeaven.
Then the class will be loaded, the member fields will be created and
initialised and the construction of Heaven will take place. Not until the
GatesOfHeaven are finally opened will the bounty of Heaven be available to the
Mortal Masses and the DearDeparted may be finally Judged.


LOL!

So just to get my facts right... a "shared class" in VB terminology is
the same as a basic singleton, but without the need to
"GetInstance()"? - I come from a SmallTalk/C++/Java OO backgound, and
it just looks funny.

Nov 20 '05 #10

P: n/a
Hi Andy,

Ahah! - you've already been to the C-side.

|| a "shared class" in VB terminology is the same as a basic singleton,

I don't know if it's official VB terminology - I just used it. But, yes,
it's VB's version of the singleton (a word I only learnt recently and thought
what's all the fuss over these - it's a static class!!)

I've no idea what GetInstance() is. I gave C++ a miss (I didn't like it
compared to C. >> and ::, yuk, put me right off) and I've skipped Java in
favour of C#.

Regards,
Fergus
Nov 20 '05 #11

P: n/a
Thank you all for the enlightenment.
I especially liked the Heavenly class example! <g>
--
Joe Fallon


"Fergus Cooney" <fi******@tesco.net> wrote in message
news:uW**************@tk2msftngp13.phx.gbl...
Hi Joe,

Shared members belong to the Class as well as to the instances of the
Class, so they are accessible without needing to create an instance.

You will still need to use the class name to refer to the methods. Classes can have what is called a class constructor. This is the same as for an
instance except that, being for the class itself, it has the Shared keyword. This Shared New() will be automatically called when the Class is first
accessed.

Public Class SharedClass
Sub Shared New
MsgBox ("Hey - Classy or what!!")
End Sub
Public Shared Sub DoSomething
End Sub
End Class

In use:
'Will call New if this is the first use of SharedClass
SharedClass.DoSomething

In VB.NET you can eliminate even the need to have a Shared Class by
putting the methods into a Module. A Module is syntactic sugar - behind the scenes it is a Shared Class. Because of this you don't need the Shared
keyword. Nor do you need to use the methods by giving the Module name (unless there is a clash with something else, eg another Module, that also defines
that name).

Public Module Stuff
Sub New
MsgBox ("Hey - New Stuff!!")
End Sub

Public Sub DoSomething
End Sub
End Module

In use:
DoSomething
Whether you use a Shared Class or a Module is up to you. You could pose it as a Best Practice question and get half-a-dozen opinions. ;-)

Regards,
Fergus

Nov 20 '05 #12

P: n/a
> Lol, There's also a logic argument for the opposite case. ;-)

Might be, depending on your defenition of constructor. Mine would be a
'method that is called directly after an object of the class has been
constructed'. In that case your scenario does not fit.
Shared Classes and Modules can both have their own constructors. See my
other post in this thread.


Could be..!

Freek Versteijn
Nov 20 '05 #13

P: n/a
Hi Freek,

|| .. depending on your defenition of constructor. Mine would
|| be a 'method that is called directly after an object of the class
|| has been constructed'.

Yes, I can go with that. It applies to both objects of a class (ie.
instances) and the class object (ie. the class <itself>). As soon as the class
object has been loaded and its fields initialised (ie. created), its
contructor is called.
|| .. depending on your defenition of constructor. Mine would
|| be a 'method that is called directly after an object of the class
|| has been constructed'.

I know what you mean, of course, but I would be more likely to say:

A constructor is a method that is called directly after an object of
the class
has been <created>.

because, to some people, it would sound strange that a constructor can be
called after the object is <constructed>.
|| In that case your scenario does not fit.

Somehow I can't see where we disagree. ;-)

Regards,
Fergus
Nov 20 '05 #14

P: n/a
First there are static/shared constructors in .NET. They execute the first
time the class is accessed; however, you cannot pass parameters in since it
is called automatically.

What you could try to do is to create a factory class that has a shared
method that returns your DAL object (something like CreateDAL()). This
support class would be able to hold an instance of your DAL class. The
first time it is called, the instance would be created. Subsequent calls
just returns the object itself. The only overhead is the call to the
method. Where the support class gets the connectionstring is up to you.
You would hardcode it in the class or get it from a config file, or
whatever.
"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:Oz**************@TK2MSFTNGP10.phx.gbl...
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?

If so, does that mean the constructor does not run?
If so, how will my class methods know the ConnectionString value???

Can someone please outline the best way to resolve this?
Thanks!

--
Joe Fallon


Nov 20 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.