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

passing object as parameter changes behavior

P: n/a
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
Oct 15 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.
"Beth" <Be**@discussions.microsoft.comwrote in message
news:26**********************************@microsof t.com...
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
Oct 15 '08 #2

P: n/a
On 2008-10-15, Beth <Be**@discussions.microsoft.comwrote:
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
(Air Code)

Class Outer
Private _inner As InnerClass
Private _shared As SharedClass

Public Sub New()
_shared = new SharedClass()
_inner = new InnerClass (Me)
End Sub

Class Inner
Private _parent As Outer

Sub New (ByVal parent As Outer)
_parent = parent
_parent._shared.DoMethod()
End Sub
End Class
End Class

Anyway... something like that? Or do you need the shared instance accross
multiple instances of outer/inner. I'm a little unclear of what your trying
to accomplish.

--
Tom Shelton
Oct 15 '08 #3

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

"Tom Shelton" wrote:
On 2008-10-15, Beth <Be**@discussions.microsoft.comwrote:
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

(Air Code)

Class Outer
Private _inner As InnerClass
Private _shared As SharedClass

Public Sub New()
_shared = new SharedClass()
_inner = new InnerClass (Me)
End Sub

Class Inner
Private _parent As Outer

Sub New (ByVal parent As Outer)
_parent = parent
_parent._shared.DoMethod()
End Sub
End Class
End Class

Anyway... something like that? Or do you need the shared instance accross
multiple instances of outer/inner. I'm a little unclear of what your trying
to accomplish.

--
Tom Shelton
Oct 16 '08 #4

P: n/a
Here is a definition and example page for the singleton pattern.

http://www.dofactory.com/Patterns/PatternSingleton.aspx

"Beth" <Be**@discussions.microsoft.comwrote in message
news:96**********************************@microsof t.com...
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

"Tom Shelton" wrote:
>On 2008-10-15, Beth <Be**@discussions.microsoft.comwrote:
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

(Air Code)

Class Outer
Private _inner As InnerClass
Private _shared As SharedClass

Public Sub New()
_shared = new SharedClass()
_inner = new InnerClass (Me)
End Sub

Class Inner
Private _parent As Outer

Sub New (ByVal parent As Outer)
_parent = parent
_parent._shared.DoMethod()
End Sub
End Class
End Class

Anyway... something like that? Or do you need the shared instance
accross
multiple instances of outer/inner. I'm a little unclear of what your
trying
to accomplish.

--
Tom Shelton
Oct 16 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.