468,554 Members | 1,972 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,554 developers. It's quick & easy.

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

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.

Dec 2 '05 #1
5 4016
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"han zhiyang" <ha********@discussions.microsoft.com> wrote in message
news:B5**********************************@microsof t.com...
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.

Dec 2 '05 #2
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.

"han zhiyang" <ha********@discussions.microsoft.com> wrote in message
news:B5**********************************@microsof t.com...
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.

Dec 2 '05 #3
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.

Dec 4 '05 #4
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.

"Kevin Spencer" wrote:
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.

Dec 4 '05 #5
han zhiyang <ha********@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 4 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Stephen Johns | last post: by
5 posts views Thread by Rob | last post: by
6 posts views Thread by Kay | last post: by
8 posts views Thread by Jeff S. | last post: by
14 posts views Thread by Chen Shusheng | last post: by
17 posts views Thread by Chen Shusheng | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.