Wei-Meng Lee - C# 2008 Programmer's Reference

Здесь есть возможность читать онлайн «Wei-Meng Lee - C# 2008 Programmer's Reference» весь текст электронной книги совершенно бесплатно (целиком полную версию без сокращений). В некоторых случаях можно слушать аудио, скачать через торрент в формате fb2 и присутствует краткое содержание. Город: Indianapolis, Год выпуска: 2009, ISBN: 2009, Издательство: Wiley Publishing, Inc., Жанр: Программирование, на английском языке. Описание произведения, (предисловие) а так же отзывы посетителей доступны на портале библиотеки ЛибКат.

C# 2008 Programmer's Reference: краткое содержание, описание и аннотация

Предлагаем к чтению аннотацию, описание, краткое содержание или предисловие (зависит от того, что написал сам автор книги «C# 2008 Programmer's Reference»). Если вы не нашли необходимую информацию о книге — напишите в комментариях, мы постараемся отыскать её.

C# 2008 Programmers Reference provides a concise and thorough reference on all aspects of the language. Each chapter contains detailed code samples that provide a quick and easy way to understand the key concepts covered.

C# 2008 Programmer's Reference — читать онлайн бесплатно полную книгу (весь текст) целиком

Ниже представлен текст книги, разбитый по страницам. Система сохранения места последней прочитанной страницы, позволяет с удобством читать онлайн бесплатно книгу «C# 2008 Programmer's Reference», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.

Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

//---assign a delegate to del of c1---

c1.del = new Class1.Class1Delegate(d1);

}

static private void DoSomething() {

//...

}

To use Class2, you create an instance of Class2and then assign a delegate to the evtevent using the +=operator:

static void Main(string[] args) {

//...

//---create a delegate---

Class2.Class2Delegate e2 =

new Class2.Class2Delegate(DoSomething);

Class2 c2 = new Class2();

//---assign a delegate to evt of c2---

c2.evt += new Class2.Class2Delegate(d1);

}

If you try to use the =operator to assign a delegate to the evtevent, you will get a compilation error:

c2.evt = new Class2.Class2Delegate(d1); //---error---

This important restriction of event is important because defining a delegate as an event will ensure that if multiple clients are subscribed to an event, another client will not be able to set the delegate to null (or simply set it to another delegate). If the client succeeds in doing so, all the other delegates set by other client will be lost. Hence, a delegate defined as an event can only be set with the +=operator.

Passing State Information to an Event Handler

In the preceding program, you simply raise an event in the AlarmClockclass; there is no passing of information from the class back to the event handler. To pass information from an event back to an event handler, you need to implement your own class that derives from the EventArgsbase class.

In this section, you modify the previous program so that when the set time is up, the event passes a message back to the event handler. The message is set when you instantiate the AlarmClockclass.

First, define the AlarmClockEventArgsclass that will allow the event to pass back a string to the event handler. This class must derive from the EventArgsbase class:

public class AlarmClockEventArgs : EventArgs {

public AlarmClockEventArgs(string Message) {

this.Message = Message;

}

public string Message { get; set; }

}

Next, define a delegate called AlarmClockEventHandlerwith the following signature:

public delegate void AlarmClockEventHandler(object sender, AlarmClockEventArgs e);

Replace the original TimesUpevent statement with the following statement, which uses the AlarmClockEventHandlerclass:

//---public event EventHandler TimesUp;---

public event AlarmClockEventHandler TimesUp;

Add a Messageproperty to the class so that users of this class can set a message that will be returned by the event when the time is up:

public string Message { get; set; }

Modify the onTimesUpvirtual method by changing its parameter type to the new AlarmClockEventArgsclass:

protected virtual void onTimesUp(AlarmClockEventArgs e) {

if (TimesUp != null) TimesUp(this, e);

}

Finally, modify the t_Elapsedevent handler so that when you now call the onTimesUp()method, you pass in an instance of the AlarmClockEventArgsclass containing the message you want to pass back to the event handler:

void t_Elapsed(object sender, ElapsedEventArgs e) {

if (DateTime.Now >= this.AlarmTime) {

onTimesUp(new AlarmClockEventArgs(this.Message));

t.Stop();

}

}

Here's the complete program: using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Timers;

public class AlarmClockEventArgs : EventArgs {

public AlarmClockEventArgs(string Message) {

this.Message = Message;

}

public string Message { get; set; }

}

public delegate void AlarmClockEventHandler(object sender, AlarmClockEventArgs e);

class AlarmClock {

Timer t;

public event AlarmClockEventHandler TimesUp;

protected virtual void onTimesUp(AlarmClockEventArgs e) {

if (TimesUp != null) TimesUp(this, e);

}

public DateTime AlarmTime { get; set; }

public string Message { get; set; }

public AlarmClock() {

t = new Timer(100);

t.Elapsed += new ElapsedEventHandler(t_Elapsed);

}

public void Start() {

t.Start();

}

void t_Elapsed(object sender, ElapsedEventArgs e) {

if (DateTime.Now >= this.AlarmTime) {

onTimesUp(new AlarmClockEventArgs(this.Message));

t.Stop();

}

}

}

With the modified AlarmClockclass, your program will now look like this:

namespace Events {

class Program {

static void c_TimesUp(object sender, AlarmClockEventArgs e) {

Console.WriteLine(DateTime.Now.ToShortTimeString() + ": " + e.Message);

}

static void Main(string[] args) {

AlarmClock c = new AlarmClock() {

//---alarm to sound off at 16 May 08, 9.50am---

AlarmTime = new DateTime(2008, 5, 16, 09, 50, 0, 0),

Message = "Meeting with customer."

};

c.TimesUp += new AlarmClockEventHandler(c_TimesUp);

c.Start();

Console.ReadLine();

}

}

}

Figure 7-10 shows the output when the AlarmClockfires the TimesUpevent.

C 2008 Programmers Reference - изображение 130

Figure 7-10

Summary

This chapter discussed what delegates are and how you can use them to invoke other functions, as well as how you can use delegates to implement callbacks so that your application is more efficient and responsive. One direct application of delegates is events, which make GUI operating systems such as Windows possible. One important difference between delegates and events is that you cannot assign a delegate to an event by using the =operator.

Chapter 8

Strings and Regular Expressions

One of the most common data types used in programming is the string. In C#, a string is a group of one or more characters declared using the stringkeyword. Strings play an important part in programming and are an integral part of our lives — our names, addresses, company names, email addresses, web site URLs, flight numbers, and so forth are all made up of strings. To help manipulate those strings and pattern matching, you use regular expressions, sequences of characters that define the patterns of a string. In this chapter, then, you will:

Читать дальше
Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

Похожие книги на «C# 2008 Programmer's Reference»

Представляем Вашему вниманию похожие книги на «C# 2008 Programmer's Reference» списком для выбора. Мы отобрали схожую по названию и смыслу литературу в надежде предоставить читателям больше вариантов отыскать новые, интересные, ещё непрочитанные произведения.


Отзывы о книге «C# 2008 Programmer's Reference»

Обсуждение, отзывы о книге «C# 2008 Programmer's Reference» и просто собственные мнения читателей. Оставьте ваши комментарии, напишите, что Вы думаете о произведении, его смысле или главных героях. Укажите что конкретно понравилось, а что нет, и почему Вы так считаете.

x