jaomello

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


Re: Visual C# General Problem with event...

Mark Dawson

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.






Re: Visual C# General Problem with event...

jaomello

How can i reset the delegate to null




Re: Visual C# General Problem with event...

Mark Dawson

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.






Re: Visual C# General Problem with event...

jaomello

Thanks a lot Mark!

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