Re: Sharing an instance of an object across classes
- From: Tom Shelton <tom_shelton@xxxxxxxxxxxxxxxxxx>
- Date: Wed, 29 Oct 2008 14:35:27 -0700
On 2008-10-29, Beth <Beth@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
Thanks, Tom.
It looks recursive, but I see where the new instance of the class is created
during the declaration instead of outside the class, enforcing the single
shared instance, as long as no caller decides to instantiate the class.
I got the instance reference to work without passing an instance of the
class as a parameter to other class's constructors, and that was what I
wanted.
I think what I was looking for before was some kind of 'functional'
inheritance, which doesn't seem to exist.
I was thinking when you instantiate a derived class, it 'latched on' to the
base class instance in memory, so you could call the base class's functions
at runtime and get the same results as if you'd called the base class's
functions directly, but it doesn't work that way. The derived class has its
own instance of myBase separate from any other instance, which makes sense,
because you're not guaranteed to have any instance of the base class in
memory or you could have several instances, and then which one would the
derived class 'latch on' to?
Thanks again for your response. I'd seen the 'shared' keyword before, but
not used on a new instance of itself.
I guess that's what they mean by 'singleton.'
-Beth
Actually, there is a mistake in that implementation... clsData should have a
private constructor. In other words, it should prevent any other classes from
instantiating it :)
Here is the correction...
Class clsData
Private Shared _instance As New clsData
....
Private Sub New ()
' this should probably read your connection
' information from the connectionstrings section
' of your config file
End Sub
' your public instance methods here
Public Shared ReadOnly Property Instance As clsData
Get
return _instance
End Get
End Property
End Class
Now, there is only on instance of this class. I am not saying this is the
correct architecture. The more I've read, it sounds like you are trying to
develop a n-teir type application. You might want to do some reading on that
topic.
--
Tom Shelton
.
- Follow-Ups:
- References:
- Sharing an instance of an object across classes
- From: Beth
- Re: Sharing an instance of an object across classes
- From: Tom Shelton
- Re: Sharing an instance of an object across classes
- From: Beth
- Re: Sharing an instance of an object across classes
- From: Tom Shelton
- Re: Sharing an instance of an object across classes
- From: Beth
- Sharing an instance of an object across classes
- Prev by Date: Re: Sharing an instance of an object across classes
- Next by Date: Re: Sharing an instance of an object across classes
- Previous by thread: Re: Sharing an instance of an object across classes
- Next by thread: Re: Sharing an instance of an object across classes
- Index(es):
Relevant Pages
|