Question

Hey I'm new to using event handlers and the like and I'm having a few problems with getting one working This is in my namespace:

public delegate void buttonChange(object sender, EventArgs e);

This is the form before main:

public static event buttonChange eventButtonChange;

Then within main I have:

buttonChangeListener listener = new buttonChangeListener();
eventButtonChange += new buttonChange(buttonChangeNeeded);

Then a procedure:

void buttonChangeNeeded(object sender, buttonChangeArgs e)
    {
        // The Program has moved a state forward
        switch (e.TheNumber)
        {
            case 1:
                // Probe has been purged
                this.SetBtnAutoCycle1(true);
                break;
            case 2:
                //First Auto-Cycle Complete
                this.SetBtnPerf(true);                    
                break;
            case 3:
                //Performance Test Complete
                this.SetBtnAutoCycle2(true);
                break;
            case 4:
                //Second Auto-Cycle Complete
                this.SetBtnReport(true);
                break;
            default:
                break;
        }

Then finally a separate classes:

public class buttonChangeArgs : EventArgs
{
    public readonly int TheNumber;

    public buttonChangeArgs(int num)
    {
        TheNumber = num;
    }
}
public class buttonChangeListener
{
    public void changeTheButton(object o, buttonChangeArgs e)
    {
        Console.WriteLine(
            "The button should move down too: {0}",
            e.TheNumber);
    }
}

To be honest I've looked through a through tutorials and tried to emulate them but don't really understand them, including the MSDN guide, I'm studying at university but promised a family member I'd develop some software for them over summer and so far it's gone well but I've hit a brick wall.

Thanks

Was it helpful?

Solution

You have declared an delegate with second parameter of type EventArgs. However in your main you are subscribing to it with handler that does not accept EventArgs but rather its subclass. Compiler cannot make a conversion here.

You can solve this by declaring delegate to accept your specific arguments:

public delegate void buttonChange(object sender, buttonChangeArgs e);

Besides that most likely your Main is a static method (I assume you are writing a console application). In that case your usage of buttonChangeNeeded is not valid since it is not static (at least from what we can see).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top