How to use delegation?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
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 said:
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.
 
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?
 
Hi Phill,
Thanks for the explanation.
Pls read my answer to alex.
I think the best way is to use property in order to hide the child form and
that's it.
--
Thanks,

Mayer


Phill W. said:
Mayer said:
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.
 
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/library/system.reflection.propertyinfo.setvalue(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?
 
Yes, thank you very much.
--
Mayer


AlexS said:
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/library/system.reflection.propertyinfo.setvalue(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?
 
Back
Top