passing object as parameter changes behavior

B

Beth

Hello.

I had a class that worked, but I knew wasn't designed correctly from an OO
point of view, so I changed it so it was more like I thought it 'should' be,
and now it doesn't always work. Works most of the time, but not always.

At first, I had an object variable declared in a class, and everything
worked fine.
Then, I figured I should really have 2 classes- one wrapping the other. I
need my object in both classes, so I passed it as a parameter to the
constructor in the new class, and that changed the behavior of the object.

Ideally, I'd like to retain the instance of the object in the outer class
and refer to it in the inner class instead of passing it as a parameter, but
I'm not sure how to do that (refer to an object or property in the class
holding an instance of me.)

What I want to do is:
Friend class clsOuter
Private m_objShared as clsShared
Private m_objInner as clsInner

Friend sub init()
m_objShared = new clsShared
m_objInner = new clsInner
end sub

friend readonly property sharedObj() as clsShared
get
return m_objShared
end get
end property
end class

Friend class clsInner
Private m_objShared as clsShared

Friend sub new()
m_objShared = myParents.objShared
m_objShared.doMethod

Is there another way I'm supposed to share a single instance of a class
across multiple classes?

Thanks for the help,

-Beth
 
F

Family Tree Mike

Your approach seems fine to me. If the class clsShared is truely a single
instance object, then another approach would be to follow the Singleton
class design pattern. You have not made it clear if that would work for you
or not though.
 
B

Beth

It took me awhile to think of an example, but what I want is something like
the relationship between a form class and the application object.

The application object contains a collection of form objects, and yet within
a form class, you're able to reference the containing application object's
properties. There's also only one instance of the application object.

I'm not sure what a 'singleton' is, but I can look into that, too.

Thanks for the replies.

-Beth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top