Problem with event...

Hi !

Well... I do this to add a function on a event:
dataRecebe.dadosRecebidos += new dataRecebe.delDadosRecebidos(atualiza_ligado);

and this to take off..
dataRecebe.dadosRecebidos -= new dataRecebe.delDadosRecebidos(atualiza_ligado);

Correct !

But if I add many function for determinate event... I will always do "... -= ..." don't have another way

Hug,
Joao


Answer this question

Problem with event...

  • Eric van Feggelen

    Hi,

    the only way to remove an event is using the -= syntax. However if you define you own event i.e.

    event X

    {

    add{...}

    remove{...}

    }

    You can get access the the underlying delegate and then clear out all of the event listeners by resetting the delegate to null.

    Mark.



  • Fusion54

    How can i reset the delegate to null

  • li4li4tsai4

    Thanks a lot Mark!

    Obs.: To invoke the event: myEventHandler.invoke();

  • kevlar623

    Hi,

    you will have to define the event manually, i.e.:

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    Test t = new Test();
    t.MyEvent += new Test.MyEventHandler(t_MyEvent);

    t.ClearAllListeners();
    }

    static void t_MyEvent()
    {

    }
    }

    class Test
    {

    public delegate void MyEventHandler();
    private MyEventHandler myEventHandler = null;

    public event MyEventHandler MyEvent
    {
    add
    {
    myEventHandler = (MyEventHandler)Delegate.Combine(myEventHandler, value);
    }
    remove
    {
    myEventHandler = (MyEventHandler)Delegate.Remove(myEventHandler, value);
    }
    }

    public void ClearAllListeners()
    {
    if (myEventHandler != null)
    {
    myEventHandler = null;
    }
    }
    }
    }

    Mark.



  • Problem with event...