PC Review


Reply
Thread Tools Rate Thread

Anonymous Methods and Delegate Declaration

 
 
Andrew Robinson
Guest
Posts: n/a
 
      24th Apr 2007
I have just started playing around with anonymous types and am really liking
it! Question: Given the following piece of code, do I have to declare the
"ControlDelegate"? Is there something that I could be using that would be a
bit more general purpose here and elseware? Possibly using a generic type?

Thanks

>>>>


SearchControls(e.Row.Controls, delegate(Control control) {
Button button = control as Button;
if (button != null) {
if (button.CommandName == "Delete") {
button.Enabled = false;
}
}
});

private void SearchControls(ControlCollection controls, ControlDelegate
controlDelegate) {
foreach (Control control in controls) {
if (control.HasControls()) {
SearchControls(control.Controls, controlDelegate);
}
controlDelegate(control);
}
}

private delegate void ControlDelegate(Control control);

 
Reply With Quote
 
 
 
 
Mattias Sjögren
Guest
Posts: n/a
 
      24th Apr 2007
Andrew,

>Is there something that I could be using that would be a
>bit more general purpose here and elseware? Possibly using a generic type?


There are some generic delegate types in the base class libraries you
can use, I guess the one that would work best here is void
Action<T>(T). So you could replace your ControlDelegate with an
Action<Control>, but that wouldn't exactly make the rest of your code
any more general purpose.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 
Reply With Quote
 
Andrew Robinson
Guest
Posts: n/a
 
      24th Apr 2007
How is Action<> different than Predicate<> ?

-A


"Mattias Sjögren" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Andrew,
>
>>Is there something that I could be using that would be a
>>bit more general purpose here and elseware? Possibly using a generic type?

>
> There are some generic delegate types in the base class libraries you
> can use, I guess the one that would work best here is void
> Action<T>(T). So you could replace your ControlDelegate with an
> Action<Control>, but that wouldn't exactly make the rest of your code
> any more general purpose.
>
>
> Mattias
>
> --
> Mattias Sjögren [C# MVP] mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.


 
Reply With Quote
 
Walter Wang [MSFT]
Guest
Posts: n/a
 
      25th Apr 2007
Hi Andrew,

The Predicate generic delegate returns a boolean while the Action generic
delegate doesn't return a value.

Regards,
Walter Wang ((E-Mail Removed), remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delegate help for Func<T, TResult> declaration Schizoid Man Microsoft C# .NET 3 1st Apr 2009 04:18 PM
Anonymous Delegate Stefan Hoffmann Microsoft C# .NET 5 29th Aug 2008 06:39 PM
Where to declare a delegate declaration Tony Johansson Microsoft C# .NET 2 4th Dec 2007 11:32 PM
Anonymous Method vs. Anonymous Delegate Robert Howells Microsoft C# .NET 3 21st Oct 2007 08:08 AM
Delegate Declaration Guidelines C# Learner Microsoft C# .NET 3 16th Mar 2004 10:55 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:57 AM.