This print for content only-size & color not accurate 7" X 9-1/4" / casebound / malloy



Download 21,46 Mb.
Pdf ko'rish
bet728/1830
Sana28.09.2021
Hajmi21,46 Mb.
#188411
1   ...   724   725   726   727   728   729   730   731   ...   1830
Bog'liq
[book] [en] Pro C# 2008 and the NET 3.5 Platform Fourth Edition



Source Code

The CarEvents project is located under the Chapter 11 subdirectory.



A “Prim-and-Proper” Event

Truth be told, there is one final enhancement we could make to the current iteration of the Car type

that mirrors Microsoft’s recommended event pattern. As you begin to explore the events sent by a

given type in the base class libraries, you will find that the first parameter of the underlying delegate

is a System.Object, while the second parameter is a type deriving from System.EventArgs.

C H A P T E R   1 1  



D E L E G AT E S, E V E N T S, A N D   L A M B D A S



367

8849CH11.qxd  10/1/07  10:43 AM  Page 367




The System.Object argument represents a reference to the object that sent the event (such as

the Car), while the second parameter represents information regarding the event at hand. The

System.EventArgs base class represents an event that is not sending any custom information:

public class EventArgs

{

public static readonly System.EventArgs Empty;



public EventArgs();

}

For simple events, you can pass an instance of EventArgs directly. However, when you wish to



pass along custom data, you should build a suitable class deriving from EventArgs. For our example,

assume we have a class named CarEventArgs, which maintains a string representing the message

sent to the receiver:

public class CarEventArgs : EventArgs

{

public readonly string msg;



public CarEventArgs(string message)

{

msg = message;



}

}

With this, we would now update the CarEventHandler delegate as follows (the events would be



unchanged):

public class Car

{

public delegate void CarEventHandler(object sender, CarEventArgs e);



...

}

Here, when firing our events from within the Accelerate() method, we would now need to



supply a reference to the current Car (via the this keyword) and an instance of our CarEventArgs

type. For example, consider the following update:

public void Accelerate(int delta)

{


Download 21,46 Mb.

Do'stlaringiz bilan baham:
1   ...   724   725   726   727   728   729   730   731   ...   1830




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