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

How to use delegation?

P: n/a
Hi,
I'm using two form classes and I would like all methods of the second class
(the child class) to be managed by the first class (the main class).
Is delegation the best solution for me?
If so, how can I define delegation?
--
Thanks,

Mayer
Jul 18 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
If you inherit child from main you will get all main methods automatically
in child, if I got your question right.

Why do you mention delegation? Do you have some specific example in mind
which made you consider delegation?
"Mayer" <me*********@dds-security.com.donotspamwrote in message
news:E2**********************************@microsof t.com...
Hi,
I'm using two form classes and I would like all methods of the second
class
(the child class) to be managed by the first class (the main class).
Is delegation the best solution for me?
If so, how can I define delegation?
--
Thanks,

Mayer


Jul 18 '07 #2

P: n/a
Mayer wrote:
I'm using two form classes and I would like all methods of the second class
(the child class) to be managed by the first class (the main class).
Is delegation the best solution for me?
No. Inheritance would be the way to go.

Have Second inherit from First and both will do exactly the same thing
until you start overriding and changing Second.
If so, how can I define delegation?
Delegate methods (known in other languages as Function Pointers) are a
way for you to call a method at run-time without knowing anything about
it at compile time, other than it has a given signature.

Event handlers are all delegates under the covers - the Control that
raises the event (and calls your Event Handler [delegate] method) knows
nothing whatever about your class and yet it can still call your method!

HTH,
Phill W.
Jul 19 '07 #3

P: n/a
Hi Alex,
Thank you for your answer.
My case is as follows:
The main form, opens the child form, nothing to do with inheritance.
I odn't want to dispose the inner class, the child. It means, instead of
calling childForm.fillColor, I prefer to activate mainForm.FillColor which
activates the inner private fillColor.

Is delegate the best solution?

--
Thanks,

Mayer
"AlexS" wrote:
If you inherit child from main you will get all main methods automatically
in child, if I got your question right.

Why do you mention delegation? Do you have some specific example in mind
which made you consider delegation?
"Mayer" <me*********@dds-security.com.donotspamwrote in message
news:E2**********************************@microsof t.com...
Hi,
I'm using two form classes and I would like all methods of the second
class
(the child class) to be managed by the first class (the main class).
Is delegation the best solution for me?
If so, how can I define delegation?
--
Thanks,

Mayer


Jul 19 '07 #4

P: n/a
I believe that depends how you define "best"

If that's one property you can just call child property from code handling
change of main form property. No need for delegation.

If you want to implement this functionality for all properties of child
form, you need to look into reflection (see PropertyInfo.SetValue at
http://msdn2.microsoft.com/en-us/lib...ue(VS.71).aspx)

Again, I don't see the need for delegation. You can add method to child,
which could be called with property name and new value.

However, even in this case I would consider inheritance and put property
change handling method into base class (for child), in case you might need
different children with same behavior.

Does this answer your question?

"Mayer" <me*********@dds-security.com.donotspamwrote in message
news:22**********************************@microsof t.com...
Hi Alex,
Thank you for your answer.
My case is as follows:
The main form, opens the child form, nothing to do with inheritance.
I odn't want to dispose the inner class, the child. It means, instead of
calling childForm.fillColor, I prefer to activate mainForm.FillColor which
activates the inner private fillColor.

Is delegate the best solution?

--
Thanks,

Mayer
"AlexS" wrote:
>If you inherit child from main you will get all main methods
automatically
in child, if I got your question right.

Why do you mention delegation? Do you have some specific example in mind
which made you consider delegation?
"Mayer" <me*********@dds-security.com.donotspamwrote in message
news:E2**********************************@microso ft.com...
Hi,
I'm using two form classes and I would like all methods of the second
class
(the child class) to be managed by the first class (the main class).
Is delegation the best solution for me?
If so, how can I define delegation?
--
Thanks,

Mayer




Jul 19 '07 #5

P: n/a
Yes, thank you very much.
--
Mayer
"AlexS" wrote:
I believe that depends how you define "best"

If that's one property you can just call child property from code handling
change of main form property. No need for delegation.

If you want to implement this functionality for all properties of child
form, you need to look into reflection (see PropertyInfo.SetValue at
http://msdn2.microsoft.com/en-us/lib...ue(VS.71).aspx)

Again, I don't see the need for delegation. You can add method to child,
which could be called with property name and new value.

However, even in this case I would consider inheritance and put property
change handling method into base class (for child), in case you might need
different children with same behavior.

Does this answer your question?

"Mayer" <me*********@dds-security.com.donotspamwrote in message
news:22**********************************@microsof t.com...
Hi Alex,
Thank you for your answer.
My case is as follows:
The main form, opens the child form, nothing to do with inheritance.
I odn't want to dispose the inner class, the child. It means, instead of
calling childForm.fillColor, I prefer to activate mainForm.FillColor which
activates the inner private fillColor.

Is delegate the best solution?

--
Thanks,

Mayer
"AlexS" wrote:
If you inherit child from main you will get all main methods
automatically
in child, if I got your question right.

Why do you mention delegation? Do you have some specific example in mind
which made you consider delegation?
"Mayer" <me*********@dds-security.com.donotspamwrote in message
news:E2**********************************@microsof t.com...
Hi,
I'm using two form classes and I would like all methods of the second
class
(the child class) to be managed by the first class (the main class).
Is delegation the best solution for me?
If so, how can I define delegation?
--
Thanks,

Mayer




Jul 23 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.