Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming
Delphi for .NET FCL Examples Reference
Delegate namespace
System
Declaration
[Serializable]
[ClassInterface(ClassInterfaceType.AutoDual)]
Delegate = class abstract(System.Object, ICloneable, ISerializable);
Description
Represents a delegate, which is a data structure that refers to a static method or to a class instance and an instance method of that class.

Note: The closest equivalent of a delegate in Delphi Win32 is a method pointer.

Example
program System_Delegate;

{$APPTYPE CONSOLE}
{$AUTOBOX ON}

type
  myMethodDelegate = function(myInt: Integer): string 
                     of object;

  mySampleClass = class
  public
    function myStringMethod (myInt: Integer): string;
    class function mySignMethod 
                   (myInt: Integer): string; static;
  end;

  SamplesDelegate = class
  public
    class procedure Main;
  end;

{ SamplesDelegate }

class procedure SamplesDelegate.Main;
var
  mySC: mySampleClass;
  myD1: myMethodDelegate;
  myD2: myMethodDelegate;
begin
  mySC := mySampleClass.Create;
  myD1 := mySC.myStringMethod;
  myD2 := mySC.mySignMethod;

  Console.WriteLine('{0} is {1}; use the sign "{2}".', 
                     5, myD1(5), myD2(5));
  Console.WriteLine('{0} is {1}; use the sign "{2}".', 
                     -3, myD1(-3), myD2(-3));
  Console.WriteLine('{0} is {1}; use the sign "{2}".',
                     0, myD1(0), myD2(0));
end;

{ mySampleClass }

class function mySampleClass.mySignMethod
         (myInt: Integer): string;
begin
  Result := '';
  if (myInt > 0) then Result := '+';
  if (myInt < 0) then Result := '-';
end;

function mySampleClass.myStringMethod
         (myInt: Integer): string;
begin
  Result := 'zero';
  if (myInt > 0) then Result := 'positive';
  if (myInt < 0) then Result := 'negative';
end;

//test SamplesDelegate
begin
  SamplesDelegate.Main;
end.
Example output
5 is positive; use the sign "+".
-3 is negative; use the sign "-".
0 is zero; use the sign "".

Looking for Delphi for .NET demos / source code samples? Look no further, Delphi for .NET FCL Examples Reference is what you are searching for!

Explore Delphi Programming

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.