c# - how would i call this function with a delegate as a parameter -
i have repetitive code trying refactor generic function generate list of checkboxes list of objects (all lists of inamed).
the second parameter delegate call function can't figure out how call method. best way call method delegate? (i looking example of code call checkboxlist function)
public delegate bool hashandler(inamed named);
here generic method
static public string checkboxlist(iqueryable<inamed> allitems, hashandler has, string name) { stringbuilder b = new stringbuilder(); foreach (var item in allitems) { if (has(item)) { b.append("<input type='checkbox' class='checkboxes' name='" + name + "' value=" + item.id + " checked />" + item.name); } else { b.append("<input type='checkbox' class='checkboxes' name='" + name + "' value=" + item.id + " />" + item.name); } } return b.tostring(); }
you're doing now:
if (has(item)) // calls delegate
that calls delegate within method. syntax have correct, , should work.
as calling checkboxlist
- sounds need have delegate defined. can method takes "inamed" argument, , returns boolean value. example, if had:
private bool myhandler(inamed named) { return true; }
you call with:
string result = checkboxlist(items, myhandler, "foo");
alternatively, pass lambda here:
string result = checkboxlist(items, named => { return (named.foo > 3); }, "foo");
Comments
Post a Comment