Indexed properties

Top  Previous  Next

What is translated > Properties > Indexed properties

 

Values which are specified  by an index can be set or get by an indexed property. The index either can be a constant as in the example below or a variable as in the example following afterwards:

 

  TRectangle = class

  private

    fCoords: array[0..3] of LongInt;

    function  GetCoord(Index: Integer): LongInt;

    procedure SetCoord(Index: Integer; Value: LongInt);

  public

    Property Left   : LongInt Index 0 read GetCoord write SetCoord;

    Property Top    : LongInt Index 1 read GetCoord write SetCoord;

 

    Property Right  : LongInt Index 2 read GetCoord write SetCoord;

    Property Bottom : LongInt Index 3 read GetCoord write SetCoord;

  end;

 

->

 

public class TRectangle : TObject

{

  private int[] fCoords = new int[4/*# range   0..  3*/];

  //# private int GetCoord(int Index);

  //# private void SetCoord(int Index, int Value);

  /*property Left : int read GetCoord write SetCoord;*/

  public int Left

  {

    get

    {

      return GetCoord(0);

    }

    set

    {

      SetCoord(0, value);

    }

  }

  /*property Top : int read GetCoord write SetCoord;*/

  public int Top

  {

    get

    {

      return GetCoord(1);

    }

    set

    {

      SetCoord(1, value);

    }

  }

  /*property Right : int read GetCoord write SetCoord;*/

  public int Right

  {

    get

    {

      return GetCoord(2);

    }

    set

    {

      SetCoord(2, value);

    }

  }

  /*property Bottom : int read GetCoord write SetCoord;*/

  public int Bottom

  {

    get

    {

      return GetCoord(3);

    }

    set

    {

      SetCoord(3, value);

    }

  }

 

  public TRectangle() {}

};

 

 

The get and set accessors are looking similar as those of simple values, but there is an additional constant parameter in the called getter and setter methods.

 

If the index value isn't constant one might think, that the C# indexer notation could be a good candidate as counterpart in C#. However there can be only one indexer in a C# class, but there can be several indexed properties in Delphi. Therefore Delphi2C# reserves the indexer syntax for the Delphi default array-property. For other indexed properties with variable index Delphi2C# creates two public methods which redirect the parameters to the private getter and setter methods. The name of these additional methods are constructed from the name of the C# property with a prefix, which can be set in the project options. The default suffices are "Readproperty" and "Writeproperty":

 

TRectangle = class

private

  fCoords: array[0..3] of LongInt;

  function  GetCoord(Index: Integer): LongInt;

  procedure SetCoord(Index: Integer; Value: LongInt);

public

  Property Coords[Index: Integer] : LongInt read GetCoord write SetCoord;

end;

 

 

->

 

public class TRectangle : TObject

{

  private int[] fCoords = new int[4/*# range   0..  3*/];

  //# private int GetCoord(int Index);

  //# private void SetCoord(int Index, int Value);

  /*property Coords [Index: integer]: int read GetCoord write SetCoord;*/

  public int ReadPropertyCoords(int Index) { return GetCoord(Index);}

  public void WritePropertyCoords(int Index, int Value){SetCoord(Index, Value);}

 

  public TRectangle() {}

};

 

 



This page belongs to the Delphi2C# Documentation

Delphi2C# home  Content