illustrates the use of an event : Event Handler « Language Basics « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Language Basics » Event HandlerScreenshots 
illustrates the use of an event
illustrates the use of an event

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example12_4.cs illustrates the use of an event
*/

using System;


// declare the MeltdownEventArgs class (implements EventArgs)
class MeltdownEventArgs : EventArgs
{

  // declare a private field named message
  private string message;

  // define a constructor
  public MeltdownEventArgs(string message)
  {
    this.message = message;
  }

  // define a property to get the message
  public string Message
  {
    get
    {
      return message;
    }
  }

}


// declare the Reactor class
class Reactor
{

  // declare a private field named temperature
  private int temperature;

  // declare a delegate class named MeltdownHandler
  public delegate void MeltdownHandler(
    object reactor,
    MeltdownEventArgs myMEA
  );

  // declare an event named OnMeltdown
  public event MeltdownHandler OnMeltdown;

  // define a property to set the temperature
  public int Temperature
  {
    set
    {
      temperature = value;

      // if the temperature is too high, the reactor melts down
      if (temperature > 1000)
      {
        MeltdownEventArgs myMEA =
          new MeltdownEventArgs("Reactor meltdown in progress!");
          OnMeltdown(this, myMEA);
      }
    }
  }

}


// declare the ReactorMonitor class
class ReactorMonitor
{

  // define a constructor
  public ReactorMonitor(Reactor myReactor)
  {
    myReactor.OnMeltdown +=
      new Reactor.MeltdownHandler(DisplayMessage);
  }

  // define the DisplayMessage() method
  public void DisplayMessage(
    object myReactor, MeltdownEventArgs myMEA
  )
  {
    Console.WriteLine(myMEA.Message);
  }

}


public class Example12_4
{

  public static void Main()
  {

    // create a Reactor object
    Reactor myReactor = new Reactor();

    // create a ReactorMonitor object
    ReactorMonitor myReactorMonitor = new ReactorMonitor(myReactor);

    // set myReactor.Temperature to 100 degrees Centigrade
    Console.WriteLine("Setting reactor temperature to 100 degrees Centigrade");
    myReactor.Temperature = 100;

    // set myReactor.Temperature to 500 degrees Centigrade
    Console.WriteLine("Setting reactor temperature to 500 degrees Centigrade");
    myReactor.Temperature = 500;

    // set myReactor.Temperature to 2000 degrees Centigrade
    // (this causes the reactor to meltdown)
    Console.WriteLine("Setting reactor temperature to 2000 degrees Centigrade");
    myReactor.Temperature = 2000;

  }

}


           
       
Related examples in the same category
1.Demonstrate passing an object to an event handler and performing the proper cast in the methodDemonstrate passing an object to an event handler and performing the proper cast in the method
2.Shows how multiple objects may subscribe to the same eventShows how multiple objects may subscribe to the same event
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.