.NET
  Home arrow .NET arrow Page 9 - The Delphi Language, Part 2
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

The Delphi Language, Part 2
By: Xavier Pacheco
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 14
    2004-07-13

    Table of Contents:
  • The Delphi Language, Part 2
  • Out Parameters, Constant Parameters, and Open Array Parameters
  • Scope and Units and Namespaces
  • The uses Clause and Circular Unit References
  • Object-Oriented Programming
  • Using Delphi Objects
  • Methods
  • Class References and Properties
  • Events
  • Visibility Specifiers, Friend Classes and Class Helpers
  • Nested Types, Operator Overloading and Attributes
  • Interfaces
  • Using Interfaces, SEH
  • Exception Classes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    The Delphi Language, Part 2 - Events


    (Page 9 of 14 )

    The Delphi language supports two different kinds of events: singleton and multicast.

    Singleton events have been in the Delphi language since the beginning. They are declared as a property whose type is a procedure type with read and write accessors. Singleton events may have zero or one event listeners. The assignment operator is used to hook a listener to the event, and nil is assigned to remove the listener from the event. Listing 5.3 provides a demonstration of the declaration and use of a singleton event.

    Listing 5.3 Singleton Event Demonstration

    1: program singleevent;
    2:
    3: {$APPTYPE CONSOLE}
    4:
    5: type
    6: TMyEvent = procedure (Sender: TObject; Msg: string) of object;
    7:
    8: TClassWithEvent = class
    9: private
    10: FAnEvent: TMyEvent;
    11: public
    12: procedure FireEvent;
    13: property AnEvent: TMyEvent read FAnEvent write FAnEvent;
    14: end;
    15:
    16: TListener = class
    17: procedure EventHandler(Sender: TObject; Msg: string);
    18: end;
    19:
    20: { TClassWithEvent }
    21:
    22: procedure TClassWithEvent.FireEvent;
    23: begin
    24: if Assigned(FAnEvent) then
    25: FAnEvent(Self, '*singleton event*');
    26: end;
    27:
    28: { TListener }
    29:
    30: procedure TListener.EventHandler(Sender: TObject; Msg: string);
    31: begin
    32: WriteLn('Event was fired. Message is: ', Msg);
    33: end;
    34:
    35: var
    36: L: TListener;
    37: CWE: TClassWithEvent;
    38: begin
    39: L := TListener.Create; // create objects
    40: CWE := TClassWithEvent.Create;
    41: CWE.AnEvent := L.EventHandler; // assign event handler
    42: CWE.FireEvent; // cause event to fire
    43: CWE.AnEvent := nil; // disconnect event handler
    44: ReadLn;n
    45: end.

    The output of the program shown in Listing 5.3 is

    Event was fired. Message is: *singleton event*

    Multicast events have been added to the language to support .NET's capability of having multiple listeners for a given event. A multicast event is a property whose type is a procedure type and requires both add and remove accessors. Multicast events can have any number of listeners. The Include() and Exclude() procedures are used to add and remove listeners from a multicast event.

    Listing 5.4 provides an example of declaring and using a multicast event.

    Listing 5.4 Multicast Event Demonstration

    1: program multievent;
    2:
    3: {$APPTYPE CONSOLE}
    4:
    5: uses
    6: SysUtils;
    7:
    8: type
    9: TMyEvent = procedure (Sender: TObject; Msg: string) of object;
    10:
    11: TClassWithEvent = class
    12: private
    13: FAnEvent: TMyEvent;
    14: public
    15: procedure FireEvent;
    16: property AnEvent: TMyEvent add FAnEvent remove FAnEvent;
    17: end;
    18:
    19: TListener = class
    20: procedure EventHandler(Sender: TObject; Msg: string);
    21: end;
    22:
    23: { TClassWithEvent }
    24:
    25: procedure TClassWithEvent.FireEvent;
    26: begin
    27: if Assigned(FAnEvent) then
    28: FAnEvent(Self, '*multicast event*');
    29: end;
    30:
    31: { TListener }
    32:
    33: procedure TListener.EventHandler(Sender: TObject; Msg: string);
    34: begin
    35: WriteLn('Event was fired. Message is: ', Msg);
    36: end;
    37:
    38: var
    39: L1, L2: TListener;
    40: CWE: TClassWithEvent;
    41: begin
    42: L1 := TListener.Create; // create objects
    43: L2 := TListener.Create;
    44: CWE := TClassWithEvent.Create;
    45: Include(CWE.AnEvent, L1.EventHandler); // assign event handler
    46: Include(CWE.AnEvent, L2.EventHandler); // assign event handler
    47: CWE.FireEvent; // cause event to fire
    48: Exclude(CWE.AnEvent, L1.EventHandler); // disconnect event handler
    49: Exclude(CWE.AnEvent, L2.EventHandler); // disconnect event handler
    50: ReadLn;
    51: end

    The output of the program shown in Listing 5.4 is

    Event was fired. Message is: *multicast event* Event was fired. Message is: *multicast event*

    Note that attempts to use Include() to add the same method more than once to the listener list will result in the method being called multiple times.

    In order to maintain compatibility with other .NET CLR languages, the Delphi compiler will implement multicast semantics even for singleton events, creating add and remove accessors for the singleton event. In this implementation, the add() method will result in an overwrite of the existing value.

    This chapter is from Delphi for .NET Developer's Guide, by Xavier Pacheco (Sams, 2004, ISBN: 0-672-32443-1). Check it out at your favorite bookstore today.

    Buy this book now.

    More .NET Articles
    More By Xavier Pacheco


     

    .NET ARTICLES

    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT