Use as events

Top  Previous  Next

New features since Delphi 7 > Anonymous Methods > Use as events

 

 

Method reference types can be used as a kind of event in Delphi and become delegates by translation to C#.

 

type

  TAnProc = Reference to procedure;

  

  TAn4Component = class(TComponent)

  private

    FMyEvent: TAnProc;

  public

    property MyEvent: TAnProc Read FMyEvent Write FMyEvent;

  end;

                           

 

procedure TestAnonymous4;

var

  C : TAn4Component;

begin

  C := TAn4Component.Create;

  C.MyEvent := procedure

  begin

    ; 

  end;

end;

 

->

 

 

  using static anonymous4.anonymous4Class;

 

public class TAn4Component : TObject

{

  private TAnProc FMyEvent;

  public bool FResult;

    // MyEvent property serves as an event:

  /*property MyEvent : TAnProc read FMyEvent write FMyEvent;*/

  public TAnProc MyEvent

  {

    get

    {

      return FMyEvent;

    }

    set

    {

      FMyEvent = value;

    }

  }

 

  public TAn4Component() {}

};

 

public class anonymous4Class

{

public delegate void TAnProc();

public static void TestAnonymous4()

{

  TAn4Component c = null;

  c = new TAn4Component();

  c.MyEvent = delegate()

  {

    ...

  };

 

 

 

 

 

 

 

 

 

 

 



This page belongs to the Delphi2C# Documentation

Delphi2C# home  Content