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

C# / C Sharp
1. 2D Graphics
2. Class Interface
3. Collections Data Structure
4. Components
5. Data Types
6. Database ADO.net
7. Design Patterns
8. Development Class
9. Event
10. File Stream
11. Generics
12. GUI Windows Form
13. Language Basics
14. LINQ
15. Network
16. Office
17. Reflection
18. Regular Expressions
19. Security
20. Services Event
21. Thread
22. Web Services
23. Windows
24. XML
25. XML LINQ
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorial
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
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
w___w_w.__j___a_v___a2s___._co__m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.