473,395 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

passing object as parameter changes behavior

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
4 1761
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andy | last post by:
Hi Could someone clarify for me the method parameter passing concept? As I understand it, if you pass a variable without the "ref" syntax then it gets passed as a copy. If you pass a...
20
by: Gregory Piñero | last post by:
Hey guys, would someone mind giving me a quick rundown of how references work in Python when passing arguments into functions? The code below should highlight my specific confusion: <code> ...
6
by: bob_jenkins | last post by:
{ const void *p; (void)memset((void *)p, ' ', (size_t)10); } Should this call to memset() be legal? Memset is of type void *memset(void *, unsigned char, size_t) Also, (void *) is the...
4
by: Sahil Malik [MVP] | last post by:
Okay so now I understand (surprised though) - that WebServices can indeed pass ByRef/ref parameters. All I have to do is mark an integer parameter of a WebMethod as "ref". Funnily enough, this is...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
17
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
7
by: Jason | last post by:
Hello I've got a very simple C# app, that has a datagrid, a text box, and a button which when clicked opens a second form... Form2 frm2 = new Form2(); frm2.Show(); When I place a datagrid,...
12
by: dave_dp | last post by:
Hi, I have just started learning C++ language.. I've read much even tried to understand the way standard says but still can't get the grasp of that concept. When parameters are passed/returned...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.