I need explanations.. why the following code give an: Parameter count mismatch ?
C# Code:
//...
public delegate int FindInRichTextBoxMethod(RichTextBox rtx, string target, int index);
public int FindInRichTextBox(RichTextBox rtx, string target, int index)
{
return rtx.Find(target, index, RichTextBoxFinds.None);
}
// ...
int start;
string tempState = "foo";
if (lista.InvokeRequired) {
object find = Invoke((FindInRichTextBoxMethod)delegate
{
return FindInRichTextBox(list, tempState, len);
});
start = (int)find;
} else {
start = FindInRichTextBox(list, tempState, len);
}
Thanks in advance.
The arguments to
Invoke()include a delegate, and the arguments passed to that delegate. You’re attempting to pass aFindInRichTextBoxMethoddelegate, but that delegate type takes three arguments. You need to:FindInRichTextBoxmethod, and thenSomething like this:
Another route is to pass in a closure, sort of like you’re attempting in your sample. In your case the error is due to the cast to a
FindInRichTextBoxMethod, so the Invoke is expecting arguments. Instead, you could ignore the cast and pass in an anonymous delegate like this:This won’t work, though, because the compiler can’t determine exactly what you want to do with that anonymous delegate. Similarly, a lambda can’t be automatically converted either:
To see why and how to fix the problem, read Why must a lambda expression be cast when supplied as a plain Delegate parameter.