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

Child object calling dynamic-parent method

P: n/a
how to work out a design to enable a child object to call its
parent(creator/container) method/property?

Its easy to be done if the child can know/assume its parent, but in my
scenario, many different object can create the same child object,
means the child object's parent is dynamic.

Event is possible, but I need the process in synchornous, as event
process is in asyn.

Class Parent()
{
Child objChild;
Public CallMeBack() {}
}
Class Parent2()
{
Child objChild;
Public CallMeBack() {}
}
Class Parent3()
{
Child objChild;
Public CallMeBack() {}
}

Class Child()
{
? objParent;
objParent.CallMeBack();
}

I attempt to use typecasting and interface, but the Child must be
well-aware of the type of the parent berfore the typecasting.
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Kelmen,
Event is possible, but I need the process in synchornous, as event
process is in asyn.


No, event handlers are called synchronously by default.
I'd make the parent classes implement a common interface

interface IParent
{
void CallMeBack();
}

and then pass in a reference to it to the Child constructor.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #2

P: n/a
I'm very grateful for the helpful info. Indeed, I have tested the
approach using Interface, it works as expected.

My initial fauilure attempt with testing is because forget to
implement the interface to the parent class. Somehow the .NET IDE
compile it "successfully", so I thought the interface approach doesn't
work. Luckily I found the missed out declaration later. I thought of
using call-back approach, but the info and requirement even confuse me
much more.
Mattias Sjögren <ma********************@mvps.org> wrote in message news:<O$**************@tk2msftngp13.phx.gbl>...
Kelmen,
Event is possible, but I need the process in synchornous, as event
process is in asyn.


No, event handlers are called synchronously by default.
I'd make the parent classes implement a common interface

interface IParent
{
void CallMeBack();
}

and then pass in a reference to it to the Child constructor.

Mattias

Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.