virtual class methods

Top  Previous  Next

What is translated > Types > Records, Classes, Interfaces > Class > class methods > virtual class methods

Because there are no virtual static methods in C# Delpi2C# has an option, which allows to convert virtual class methods either to static non-virtual methods or to virtual non-static methods.

 

The first case results into the same code as for non-virtual class methods. If the virtual class methods aren't overridden, this is obviously the best option. But if the methods are overwritten, the virtual class methods have to be converted to virtual C# methods. Then these methods cannot be called through a class type expression in C# any more, If they are called that way in the Delphi code, an adequate instance of the class has to be provided in C#. If the option to create meta classes is enabled Delphi2C# provides these instances automatically:

 

 

TBase = class(TObject)

public

  class function ClassVirtual(xi: Integer): Integer; virtual;

 

 

var

  base : TBase;

begin

  base.ClassVirtual(0);

  TBase.ClassVirtual(0);

  TDerived.ClassVirtual(0);

 

->

 

public class TBase : TObject

{

  public /*#static*/ virtual int ClassVirtual(int xi)

  {

 

 

 

base.ClassVirtual(0);

ClassRef<TBase>.getClassInstance().ClassVirtual(xi);

ClassRef<TDerived>.getClassInstance().ClassVirtual(xi);

 

 

By calling ClassVirtual through the TBase pointer base, the correct version of  ClassVirtual will be called as for for non-static methods too. The correct version of ClassVirtual will be called in C# too, if the class name is used in Delphi, because Delphi2C# replaces the class names by the according class reference instances.

 

 

 

 

 

 



This page belongs to the Delphi2C# Documentation

Delphi2C# home  Content