Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Parameter count mismatch Error

DZone's Guide to

Parameter count mismatch Error

·
Free Resource

Let me share you with one of the weirdest errors I ever encountered. Recently, I have been working on a distributed application which is built from a server and some clients. The clients are Windows Forms applications. Yesterday, I spent a whole day chasing a very weird and strange error - I was getting an exception at the main method (unhandled exception) of the client application. Here is what I got:

The error description was: “Parameters count mismatch“. Well, it didn’t help me a lot, but I thought that looking at the stack trace might be a good idea. So, here it is:

As you can see, there is nothing helpful here because the stack trace is full of .Net Framework methods, none of those methods are mine. Well after a whole day, trying to understand what happened, I figured out what was wrong. Take a look at the very simplified version of my code:

delegate void ChangeColorEventHandler(object sender, Color color);

private void ChangeColor(object sender, Color newColor)
{
if(InvokeRequired)
{
BeginInvoke(new ChangeColorEventHandler(ChangeColor), newColor);
return;
}
BackColor = newColor;
}

The ChangeColor method is changing the form back color on the UI thread. This method was called due to a server request, not because of a user action. Do you see what is wrong here? I didn’t set the sender as a parameter to the BeginInvoke method. That is it! It turns out that if you get this strange error, you probably called a BeginInvoke or Invoke methods with a wrong set of parameters. After changing my code to:

private void ChangeColor(object sender, Color newColor)
{
if(InvokeRequired)
{
BeginInvoke(new ChangeColorEventHandler(ChangeColor), sender, newColor);
return;
}
BackColor = newColor;
}

Everything was fine. Hope it would be helpful for some of you one day…

 

Topics:

Published at DZone with permission of Shahar Yair. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}