C# 0 The Complete Reference


MyEvent , is created. Inside the class, an event called  SomeEvent



Download 4,07 Mb.
Pdf ko'rish
bet534/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   530   531   532   533   534   535   536   537   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

MyEvent

, is created. Inside the class, an event called 



SomeEvent

 is declared, using this line:

public event MyEventHandler SomeEvent;

Notice the syntax. The keyword 



event

 tells the compiler that an event is being declared.

Also declared inside 

MyEvent

 is the method 



OnSomeEvent( )

, which is the method a 

program will call to signal (or “fire”) an event. (That is, this is the method called when the 

event occurs.) It calls an event handler through the 



SomeEvent

 delegate, as shown here:

if(SomeEvent != null)

  SomeEvent();

Notice that a handler is called if and only if 

SomeEvent

 is not 


null

. Since other parts of your 

program must register an interest in an event in order to receive event notifications, it is 

possible that 



OnSomeEvent( )

 could be called before any event handler has been registered. 

To prevent calling on a 

null

 reference, the event delegate must be tested to ensure that it is 

not

null

.

Inside



EventDemo

, an event handler called 



Handler( )

 is created. In this simple example, 

the event handler simply displays a message, but other handlers could perform more 

meaningful actions. In 



Main( )

, a 


MyEvent

 object is created, and 



Handler( )

 is registered 

as a handler for this event, by adding it as shown here:

MyEvent evt = new MyEvent();

// Add Handler() to the event list.

evt.SomeEvent += Handler;

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   530   531   532   533   534   535   536   537   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish