473,837 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.objSh ared
m_objShared.doM ethod

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 1786
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**@discussio ns.microsoft.co mwrote in message
news:26******** *************** ***********@mic rosoft.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.objSh ared
m_objShared.doM ethod

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**@discussio ns.microsoft.co mwrote:
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.objSh ared
m_objShared.doM ethod

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**@discussio ns.microsoft.co mwrote:
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.objSh ared
m_objShared.doM ethod

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**@discussio ns.microsoft.co mwrote in message
news:96******** *************** ***********@mic rosoft.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**@discussio ns.microsoft.co mwrote:
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.objSh ared
m_objShared.doM ethod

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._share d.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
36427
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 variable with the "ref" syntax then it gets passed as a reference to the object and any changes to
20
2007
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> bool1=True lst1= def func1(arg1): arg1.append(4)
6
8272
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 generic pointer type. My real question is, is (void *) such a generic pointer type that it
4
2747
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 also supported per the SOAP Spec, and from what I understand, .NET's implementation of WebServices, donot follow the standard, but instead shimmy this behavior by working with a strict request/response WSDL. So my question is - If I mark an int...
11
8141
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 number) which is the last line of the following sub routine: ' procedure modifies elements of array and assigns ' new reference (note ByVal) Sub FirstDouble(ByVal array As Integer()) Dim i As Integer
17
2820
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 free()? #include <stdlib.h> struct stype { int *foo; };
7
3315
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 object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
7
2810
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, onto form1 I point to a DB called Test DB and then my text box, I modify the databinding property to my bindingSource which in
12
2602
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 by value temporaries are created?(I'm not touching yet the cases where standard allows optimizations from the side of implementations to avoid copying) If so, please quote part of the standard that says that. Assuming it is true, I can imagine two...
0
9682
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10877
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10273
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9402
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7806
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7001
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5670
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5849
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.