C# 0 The Complete Reference


value . By implementing  add



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

value

. By implementing 



add

 and 


remove

, you can define a custom event-handler storage scheme. For example, you could 

use an array, a stack, or a queue to store the handlers.

Here is an example that uses the accessor form of 



event

. It uses an array to hold the 

event handlers. Because the array is only three elements long, only three event handlers 

can be held in the chain at any one time.

// Create a custom means of managing the event invocation list.

using System;

// Declare a delegate type for an event.

delegate void MyEventHandler();

// Declare a class that holds up to 3 events.

class MyEvent {

  MyEventHandler[] evnt = new MyEventHandler[3];

  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 list full.");

    }


    // Remove an event from the list.

    remove {

      int i;

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

        if(evnt[i] == value) {

www.freepdf-books.com




426

 

P a r t   I :  



T h e   C #   L a n g u a g e

          evnt[i] = null;

          break;

        }

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

    }


  }

  // This is called to fire the events.

  public void OnSomeEvent() {

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

        if(evnt[i] != null) evnt[i]();

  }


}

// Create some classes that use MyEventHandler.

class W {

  public void Whandler() {

    Console.WriteLine("Event received by W object");

  }


}

class X {

  public void Xhandler() {

    Console.WriteLine("Event received by X object");

  }

}

class Y {



  public void Yhandler() {

    Console.WriteLine("Event received by Y object");

  }

}

class Z {



  public void Zhandler() {

    Console.WriteLine("Event received by Z object");

  }

}

class EventDemo5 {



  static void Main() {

    MyEvent evt = new MyEvent();

    W wOb = new W();

    X xOb = new X();

    Y yOb = new Y();

    Z zOb = new Z();

    // Add handlers to the event list.

    Console.WriteLine("Adding events.");

    evt.SomeEvent += wOb.Whandler;

    evt.SomeEvent += xOb.Xhandler;

    evt.SomeEvent += yOb.Yhandler;

www.freepdf-books.com




Download 4,07 Mb.

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