Zdar, právě se snažím pochopit události & delegáty. Mám pocit že logiku chápu.
Delegáti jsou více méně ukazatelé na metody (můžou ukazovat na více metod), list na které metody ukazují je dynamický, etc..
Události jsou seznamy metod/delgátů které jsou volány když se něco stane co vyvolá danou událost. A tady mám právě problém, odběratelem události může být i delegát, i metoda či jenom jedno z toho?
Fórum › .NET
Delegáti & události

Chápu tvé zmatení. Odběratelem události může být pouze delegát (tedy "odkaz na metodu") nikoliv metoda samotná. Co tě možná mate, je, že novější verze .netu dovolují v kódu zapsat delegáta pouze jménem metody na kterou se odkazuje. Interně to ale zůstává delegát. Je to sice jednodušší na použití, ale může být horší na pochopení. Praxe tě naučí. :)
Trochu jsem se u toho zastavil, a nejsem si jist jestli to jde i takhle:
delegate void mujDelegat();
event mujDelegat mojeUdalost = new mujDelegat(mojeMetoda());
A je také možné nechat implicitně vygenerovat delegát?
delegate void mujDelegat();
event mujDelegat mojeUdalost = mojeMetoda();
//Nedávám edit protože se mi zdá že je to až moc nesouvisející
To Petrroll :
Jenom drobnost... Je to starší vlákno ale proč to tu nechávat s chybou...
Správně je to takhle
delegate void mujDelegat();
event mujDelegat mojeUdalost = new mujDelegat(mojeMetoda);
a (nebo :) )delegate void mujDelegat();
event mujDelegat mojeUdalost = mojeMetoda;
Prostě do ukazatele se nepíší parametry() - protože ty už jsou definované jako parametry delegáta a target musí být v parametrech totožný.
Nevím, jestli jsi to jenom nevynachal kvůli příkladu, ale pokud by se sem někdy někdo progooglil tak ať je to komplet: pozor na to
event mujDelegat mojeUdalost = new mujDelegat(mojeMetoda);
To si můžeš dovolit při inicializaci (vyhneš se NullReferenceException kdyby jsi vypálil event a neměl na něm žádného delegáta, většinou se ale event před vypálením kontroluje zda != null místo aby se na něj věšel prázdnej delegát...) jenže pokud by jsi na něj chtěl navěsit víc delegátů, jako třeba
delegate void mujDelegat();
event mujDelegat mojeUdalost;
// tělo metody:
mojeUdalost = mojeMetoda1; // nebo mojeUdalost = mujDelegat(mojeMetoda1);
mojeUdalost = mojeMetoda2; // atd
mojeUdalost = mojeMetoda3;
mojeUdalost(); // tím jsem vypálil event
tak se logicky provede pouze mojeMetoda3, protože první dvě jsou přepsány. Pokud chceš na event navěsit víc delegátů než jednoho (občas se to hodí :) Nakonec, je to přece event, ne? Jinak by stačila obyč proměnná s typem mujDelegat ) tak stačí = nahradit za +=
delegate void mujDelegat();
event mujDelegat mojeUdalost;
// tělo metody:
mojeUdalost += mojeMetoda1; // nebo mojeUdalost += mujDelegat(mojeMetoda1);
mojeUdalost += mojeMetoda2; // atd
mojeUdalost += mojeMetoda3;
mojeUdalost(); // tím jsem vypálil event
a teď proběhnou všechny tři metody.

Přidej příspěvek
Toto téma je starší jak čtvrt roku – přidej svůj příspěvek jen tehdy, máš-li k tématu opravdu co říct!
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
Aktuálně jsou podporována videa ze serverů YouTube, Vimeo a Dailymotion.
×






























































Uživatelé prohlížející si toto vlákno
Uživatelé on-line: 0 registrovaných, 88 hostů
Podobná vlákna
Delegáti C# — založil plasmo
Spuštění obsluhy události (bez události) — založil bear.king
AJAX & PHP & MySQL vložení záznamu — založil JMM
BASCOM : data(1), &HFF snížit o jednu jednotku dolů na data(1),… — založil grantorino
Guild Wars & Star Wars Galaxies & MMORPG — založil Marecek
Moderátoři diskuze