C# 0 The Complete Reference


this  by the calling code. The  EventArgs



Download 4,07 Mb.
Pdf ko'rish
bet546/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   542   543   544   545   546   547   548   549   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

this

 by the calling code. The 



EventArgs

 parameter 

contains additional information and can be ignored if it is not needed.

The


EventArgs

 class itself does not contain fields that you use to pass additional data to 

a handler. Instead, 

EventArgs

 is used as a base class from which you will derive a class that 

contains the necessary fields. 

EventArgs

 does include one 



static

 field called 



Empty

, which is 

an

EventArgs

 object that contains no data.

Here is an example that creates a .NET-compatible event:

// A .NET-compatible event.

using System;

// Derive a class from EventArgs.

class MyEventArgs : EventArgs {

  public int EventNum;

www.freepdf-books.com



PART I

C h a p t e r   1 5 :  

D e l e g a t e s ,   E v e n t s ,   a n d   L a m b d a   E x p r e s s i o n s  

431


PART IPART I

}

// Declare a delegate type for an event.



delegate void MyEventHandler(object source, MyEventArgs arg);

// Declare a class that contains an event.

class MyEvent {

  static int count = 0;

  public event MyEventHandler SomeEvent;

  // This fires SomeEvent.

  public void OnSomeEvent() {

    MyEventArgs arg = new MyEventArgs();

    if(SomeEvent != null) {

      arg.EventNum = count++;

      SomeEvent(this, arg);

    }


  }

}

class X {



  public void Handler(object source, MyEventArgs arg) {

    Console.WriteLine("Event " + arg.EventNum +

                      " received by an X object.");

    Console.WriteLine("Source is " + source);

    Console.WriteLine();

  }


}

class Y {

  public void Handler(object source, MyEventArgs arg) {

    Console.WriteLine("Event " + arg.EventNum +

                      " received by a Y object.");

    Console.WriteLine("Source is " + source);

    Console.WriteLine();

  }


}

class EventDemo6 {

  static void Main() {

    X ob1 = new X();

    Y ob2 = new Y();

    MyEvent evt = new MyEvent();

    // Add Handler() to the event list.

    evt.SomeEvent += ob1.Handler;

    evt.SomeEvent += ob2.Handler;

    // Fire the event.

    evt.OnSomeEvent();

    evt.OnSomeEvent();

  }

}

www.freepdf-books.com




432

 

P a r t   I :  



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

Here is the output:

Event 0 received by an X object.

Source is MyEvent

Event 0 received by a Y object.

Source is MyEvent

Event 1 received by an X object.

Source is MyEvent

Event 1 received by a Y object.

Source is MyEvent

In this example, 


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   542   543   544   545   546   547   548   549   ...   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