17.06.2013 Views

Il linguaggio C# Eventi ed eccezioni - Dipartimento di Ingegneria ...

Il linguaggio C# Eventi ed eccezioni - Dipartimento di Ingegneria ...

Il linguaggio C# Eventi ed eccezioni - Dipartimento di Ingegneria ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Eventi</strong>: Modello d'uso<br />

A. Longheu – Tecniche <strong>di</strong> programmazione avanzata<br />

Per agganciarsi a un evento (cioè iniziare a ricevere le<br />

notifiche <strong>di</strong> un evento), un event receiver deve:<br />

definire il metodo (event handler) che verrà invocato all’atto della<br />

notifica dell’evento:<br />

void ListChang<strong>ed</strong>(object sender, EventArgs e){...}<br />

creare un delegato dello stesso tipo dell’evento e farlo puntare al<br />

metodo event handler:<br />

EventHandler ListChang<strong>ed</strong>Handler =new<br />

EventHandler(ListChang<strong>ed</strong>);<br />

aggiungere questo delegato alla lista dei delegati associati<br />

all’evento, utilizzando l’operatore +=:<br />

List.Chang<strong>ed</strong> += ListChang<strong>ed</strong>Handler ;<br />

14

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!