Parameter Count Mismatch

BSchlinker picture BSchlinker · Oct 28, 2009 · Viewed 66.4k times · Source

Having trouble with the following segment of code. I'm getting a parameter count mismatch.

I've had to write this because of problems with multiple threads and unsafe updates.


       delegate void data_INPUTDelegate(object sender, System.IO.Ports.SerialDataReceivedEventArgs e);
    private void data_INPUT(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        string data = serialPort.ReadLine();

        string[] tokens = data.Split(':');
        if (tokens[0] == "$SENSOR")
        {
            if (label_sensorValue.InvokeRequired)
            {
                data_INPUTDelegate del = new data_INPUTDelegate(data_INPUT);
                label_sensorValue.Invoke(del,new object[] {tokens[1]});
            }
            else
            {
                label_sensorValue.Text = tokens[1];
            }
        }
    }

Answer

ybo picture ybo · Oct 28, 2009

I guess the error comes from this line:

label_sensorValue.Invoke(del,new object[] {tokens[1]});

You pass only one parameter to del (tokens[1]) but it has two parameters (sender and e)

EDIT: after carefully reading your code, I suggest that you create a SetSensorValue method to set the value of label_sensorValue. Right now you're trying to invoke the event handler with wrong parameters.

private void data_INPUT(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
    string data = serialPort.ReadLine();

    string[] tokens = data.Split(':');
    if (tokens[0] == "$SENSOR")
    {
        SetSensorValue(tokens[1]);
    }
}

delegate void SetSensorValueDelegate(string value);

private void SetSensorValue(string value)
{
    if (label_sensorValue.InvokeRequired)
    {
        SetSensorValueDelegate del = new SetSensorValueDelegate(SetSensorValue);
        label_sensorValue.Invoke(del, new object[] {value});
    }
    else
    {
        label_sensorValue.Text = value;
    }
}