C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet542/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   538   539   540   541   542   543   544   545   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

MyEventHandler

is defined. Next, the class 



MyEvent

 is declared. It begins by defining a three-element array 

of event handlers called 

evnt

, as shown here:

MyEventHandler[] evnt = new MyEventHandler[3];

This array will be used to store the event handlers that are added to the event chain. The 

elements in 

evnt

 are initialized to 



null

 by default.

Next, the event 

SomeEvent

 is declared. It uses the accessor form of the 



event

 statement, 

as shown here:

public event MyEventHandler SomeEvent {

  // Add an event to the list.

  add {


    int i;

    for(i=0; i < 3; i++)

      if(evnt[i] == null) {

        evnt[i] = value;

        break;

      }


    if (i == 3) Console.WriteLine("Event queue full.");

  }


  // Remove an event from the list.

  remove {

    int i;

    for(i=0; i < 3; i++)

      if(evnt[i] == value) {

        evnt[i] = null;

        break;

      }


    if (i == 3) Console.WriteLine("Event handler not found.");

  }


}

When an event handler is added, 



add

 is called and a reference to the handler (contained in 



value

) is put into the first unused (that is, null) element of 



evnt

. If no element is free, then 

an error is reported. (Of course, throwing an exception when the list is full would be a better 

approach for real-world code.) Since 



evnt

 is only three elements long, only three event 

handlers can be stored. When an event handler is removed, 

remove

 is called and the 



evnt

array is searched for the reference to the handler passed in 



value

. If it is found, its element 

in the array is assigned 

null

, thus removing the handler from the list.

When an event is fired, 

OnSomeEvent( )

 is called. It cycles through the 



evnt

 array, 


calling each event handler in turn.

As the preceding example shows, it is relatively easy to implement a custom event-

handler storage mechanism if one is needed. For most applications, though, the default 

storage provided by the non-accessor form of 



event

 is better. The accessor-based form of 



event

 can be useful in certain specialized situations, however. For example, if you have a 

program in which event handlers need to be executed in order of their priority and not in 

the order in which they are added to the chain, then you could use a priority queue to store 

the handlers.

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   538   539   540   541   542   543   544   545   ...   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