Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 12th, 2007, 12:38 PM
run run is offline
Registered User
 
Join Date: Jul 2007
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);
          }
      }

   }
}








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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.