Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2005 > BOOK: Beginning Visual C# 2005
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Visual C# 2005
This is the forum to discuss the Wrox book Beginning Visual C# 2005 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, Eric White; ISBN: 9780764578472
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 12th, 2007, 12:38 PM
run run is offline
Registered User
 
Join Date: Jul 2007
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Ch13 Exercise Question

in the Answers to Ch13 Exercise it states the following:

"Note that you need this extra ProcessElapsedEvent() method, as the
ElapsedEventHandler delegate can't be cast to an EventHandler delegate. You don't need to do this for your MessageHandler delegate since it has a syntax identical to EventHandler:"


But when I try this excercise I don't have any problems using ProcessEvent Eventhandler for both types of events. How come? My code in Connection.cs is as follows:

 #region Using directives

using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;

#endregion

namespace Ch12Ex03
{
   public delegate void MessageHandler(object source,
                                       EventArgs e);

   public class Connection
   {
      public event MessageHandler MessageArrived;

      private string name;

      public string Name
      {
         get
         {
            return name;
         }
         set
         {
            name = value;
         }
      }

      private Timer pollTimer;

      public Connection()
      {
         pollTimer = new Timer(100);
         pollTimer.Elapsed += new ElapsedEventHandler(CheckForMessage);
         pollTimer.Elapsed += new ElapsedEventHandler(ProcessEvent);
      }

      public void Connect()
      {
         pollTimer.Start();
      }

      public void Disconnect()
      {
         pollTimer.Stop();
      }

      private static Random random = new Random();

      private void CheckForMessage(object source, ElapsedEventArgs e)
      {
         Console.WriteLine("Checking for new messages.");
         if ((random.Next(9) == 0) && (MessageArrived != null))
         {
          ProcessEvent(this, new MessageArrivedEventArgs("Hello Mum!"));
         }
      }

       // New Handler
      public void ProcessEvent(object source, EventArgs e)
      {
          if (e is MessageArrivedEventArgs)
          {
              Console.WriteLine("Connection.MessageArrived event received.");
              Console.WriteLine("Message: {0}",
              (e as MessageArrivedEventArgs).Message);
          }
          if (e is ElapsedEventArgs)
          {
              Console.WriteLine("Timer.Elapsed event received.");
              Console.WriteLine("SignalTime: {0}",
              (e as ElapsedEventArgs).SignalTime);
          }
      }

   }
}



Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ch13 Case Study 2 alecwood BOOK: Beginning Access 2003 VBA 0 October 18th, 2007 05:40 AM
VB shopping cart datalist ch13 Alexi BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 2 October 3rd, 2006 02:34 AM
Ch13 EditingData.aspx mahir BOOK: Beginning ASP.NET 1.0 8 April 28th, 2004 03:25 AM



All times are GMT -4. The time now is 02:05 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.