Assignment to a method

Top  Previous  Next

New features since Delphi 7 > Anonymous Methods > Assignment to a method

 

As well as anonymous methods can be assigned to a method reference (see above), a normal method can be assigned to it. In C# this is done by means of std::bind. The expression of this assignment looks quite complicated however, because std::placeholders are needed to represent unbound variables.

 

 

 

type

  TMethRef = Reference to procedure(X: Integer);

 

TAn3Class = class(TObject)

  procedure method(X: Integer);

end;

 

procedure Test;  

var

  m: TMethRef;

  i: TAn3Class;

begin

  // ...

  m := i.method;   

end;

 

 

->

 

typedef std::function<void (int)> TMethRef;

 

public class TAn3Class : TObject

{

  public bool Method(int x)

  {

    ...

  }

 

  public TAn3Class() {}

};

 

void Test()

{

  TMethRef m = null;

  TAn3Class i = null;

  // ...  todo: i = new TAn3Class();

  m = i.Method; 

}

 

Here remains a problem. the assignment of "i.Method" only works, if i isn't null.

 

 

 

 

 

 

 

 

 

 

 

 

 



This page belongs to the Delphi2C# Documentation

Delphi2C# home  Content