How to define and use a property of delegate type,"=" or "+="?

G

Guest

Hi.
I tried to design a custom web control which can flexibly and
dynamicly let the control user ,for example the web page developer, customize
its layout codes.This control derives from System.Web.UI.Control,and for my
purpose,I define a delegate property in the control's class definition, so
control user can define his(or her) method out of the class definition and
add his(or her) method to the delegate chain. I also override the Render
method of System.Web.UI.Control to call this delegate,so in the Rendering
stage, customized rendering logics can be implemented.
This is the question. Since I define the delegate as property(also
accompanid with a private field with the same name except the first letter
lowcasing,according to the Camel style) ,how can I deal with the "+=" and
"="? I mean maybe I have two choices:
1.Use "=" in the "set" block in property definition and tell control
users to use "+=" when they use the property in their web pages.
In class definition-->property definition:
set{
delegateName = value;
}
In web page before "Rendering" stage:
controlName.DelegateName += CustomRenderingMethod;

2.Use "+=" in the "set" block and "=" in web pages.
In class definition-->property definition:
set{
delegateName += value;
}
In web page before "Rendering" stage:
controlName.DelegateName = CustomRenderingMethod;

By the way,I think both "=" or both "+=" is either not reasonable. I
wonder what is the correct way.

Appretiate for any help.
 
N

Nicholas Paldino [.NET/C# MVP]

han,

I don't understand why you are not exposing the delegate as an event.
Either that, or define an interface, the implementation of which will return
the information you are looking for, and then assign a single instance to
your class.

Hope this helps.
 
K

Kevin Spencer

delegates are multi-cast, meaning that multiple methods CAN be assigned to
the same delegate. This does not mean that multiple methods SHOULD ALWAYS be
assigned to the same delegate. If you want multiple methods to be able to be
assigned to the same delegate, use "+=" - otherwise not.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but you can't make it stink.
 
G

Guest

Dear Mr.Paldino, :)
Thank you very much for your suggestion,and yes,event is a more friendly
way to do this,though in my condition,an event is somewhat not pure. The main
problem is that the "timing" at which the event is triggered is not
independent,for the customed Rendering method must be called as a part of the
Control's Render method,and the latter cannot be called in user's code.I
think using event,the benefit is that EventHandler has implemented the
delegate chain's "addon" and "remove" operation,and out of my control's class
definition,users can simply use "+=" to add their own Rendering method.Of
course this should be done at sometime before the "rendering" statge of this
control,for the OnEventName method must be called in
System.Web.UI.Control.Render method.
MY question is from an accasional idea.Maybe this is a bad design,and
using css or XSTL is better.
Thank you again for your help.
 
G

Guest

Hi Kevin,
Thank you for your help.I think it is my poor English misleading
you,I'm sorry.:).
This problem occurs when my delegate is a chain that can accept
severval methods.
So the problem is the correct location of "+=" operator,in property
definition ,or in user's code? That is to say,choose one from the following
two.

1.Use "=" in the "set" block in property definition and tell control
users to use "+=" when they use the property in their web pages.
In class definition-->property definition:
set{
delegateName = value;
}
In web page before "Rendering" stage:
controlName.DelegateName += CustomRenderingMethod;

2.Use "+=" in the "set" block and "=" in web pages.
In class definition-->property definition:
set{
delegateName += value;
}
In web page before "Rendering" stage:
controlName.DelegateName = CustomRenderingMethod;

Thank you again for your help.
 
J

Jon Skeet [C# MVP]

han zhiyang said:
Hi Kevin,
Thank you for your help.I think it is my poor English misleading
you,I'm sorry.:).
This problem occurs when my delegate is a chain that can accept
severval methods.
So the problem is the correct location of "+=" operator,in property
definition ,or in user's code? That is to say,choose one from the following
two.

1.Use "=" in the "set" block in property definition and tell control
users to use "+=" when they use the property in their web pages.
In class definition-->property definition:
set{
delegateName = value;
}
In web page before "Rendering" stage:
controlName.DelegateName += CustomRenderingMethod;

2.Use "+=" in the "set" block and "=" in web pages.
In class definition-->property definition:
set{
delegateName += value;
}
In web page before "Rendering" stage:
controlName.DelegateName = CustomRenderingMethod;

Thank you again for your help.

The first option is much more intuitive - it would be odd to set a
property and it actually just *add* to the existing value.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top