Using events

Sử dụng sự kiện trong việc thực hiện sau đây, bạn sẽ sử dụng sự kiện để đơn giản hóa các chương trình bạn hoàn thành trong tập thể dục đầu tiên. Bạn sẽ thêm một sự kiện vào trường lớp Mã CK và xóa Thêm và Hủy bỏ phương pháp. | Using Events In the following exercise you will use events to simplify the program you completed in the first exercise. You will add an event field to the Ticker class and delete its Add and Remove methods. You will then modify the and methods to subscribe to the event. You will also examine the Timer object used by the Ticker class to obtain a pulse once each second. Rework the digital clock application 1. Return to Visual Studio 2005 displaying the Delegates project. In the Code and Text Editor window display the source file. This file contains the declaration of the Tick delegate type in the Ticker class public delegate void Tick int hh int mm int ss 2. Add a public event called tick of type Tick to the Ticker class. The Ticker class should now look like this class Ticker public delegate void Tick int hh int mm int ss public event Tick tick . 3. Comment out the following delegate variable tickers from end of Ticker class as it is now obsolete private Tick tickers 4. Comment out the Add and Remove methods from the Ticker class. The add and remove functionality is automatically provided by the and - operators of the event. 5. In the Code and Text Editor window locate the method. This method previously invoked an instance of the Tick delegate. Modify it so that it calls the tick event instead. Don t forget to check whether tick is null before calling the event. The Notify method should look like this class Ticker . private void Notify int hours int minutes int seconds if null hours minutes seconds . Notice that the Tick delegate specifies parameters so the statement that raises the tick event must specify arguments for each of these parameters. 6. Examine the ticking variable at the end of the class private ticking new The Timer class is part of the .NET Framework. It can be programmed to repeatedly raise an event at a specified interval. Examine the .

Bấm vào đây để xem trước nội dung
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.