Re: Sharing an instance of an object across classes

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Thanks again, Tom.
I like that better.

I've read about n-tier development, but that's not really what I'm doing.
Everything is in one assembly for one project with one database. It's not a
large-scale thing, just departmental. It's not dependent on any modules
running on other servers, in other words.

"Tom Shelton" wrote:

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

.



Relevant Pages

  • Re: Sharing an instance of an object across classes
    ... I was thinking when you instantiate a derived class, ... base class instance in memory, so you could call the base class's functions ... Class clsData ... Private Shared _instance As New clsData ...
    (microsoft.public.dotnet.languages.vb)
  • Re: basic question on C++
    ... > From the point of view of a derived class, ... All I can think of is that "how do you set variables in the base class ... to be all private" is meant as if it were followed with "...to the ... That would be via private inheritance: ...
    (alt.comp.lang.learn.c-cpp)
  • Re: How Do I Prevent Copying From Base Class?
    ... > three base classes while derived class from base class is executed. ... > void Run; ... I will add them into private class later. ...
    (comp.lang.cpp)
  • Re: How to prevent a function in base class being overloaded from child class
    ... > I believe my func() in base class is the best solution for certain goal. ... Can you make funcprivate? ... Why do you care about an unrelated function in a derived class? ...
    (comp.lang.cpp)
  • Re: Application.Run() problem
    ... still am very much confused about the paint event handler. ... It most obviously should be the derived class, ... It seems that you're not understanding how inheritance works. ... which ensures that the base class uses the same ...
    (microsoft.public.dotnet.languages.csharp)