Anonymous Methods and Delegate Declaration

A

Andrew Robinson

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);
 
M

Mattias Sjögren

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
 
W

Walter Wang [MSFT]

Hi Andrew,

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

Regards,
Walter Wang ([email protected], 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.
 

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