Top  Previous  Next

What is translated > Types > Records, Classes, Interfaces > Record

A record mainly consists in public data elements, but also may have methods and sub-records. In Delphi a record also may have a variant part.



In C# a record may not have a constructor without parameters.



Delphi records can contain arrays with a fixed size, e.g..: 



  TFormatSettings = record


    ShortMonthNames: array[1..12] of string;



A C# structure however only can contain arrays with undefined size.



  public struct TFormatSettings


    public string[] ShortMonthNames;



In such and similar case Delphi2C# inserts a method called "CreateRecordMembers" into the structure, which creates the array with defined size:


public void CreateFixedArrays()


   ShortMonthNames = new string[12/*# range 1..12*/];



In addition for all structures a public static function called  "CreateRecord" is inserted:



  public static TFormatSettings CreateRecord()


    TFormatSettings tmp = new TFormatSettings();


    return tmp;




CreateRecord is similar to a Delphi constructor, but it is not called "Create", because the records often already have "Create" functions, as it is the case for TFormatSettings.


At the places where record variable are declared in the Delphi code, Delphi2C# now writes code like:


TFormatSettings result = TFormatSettings.CreateRecord();



Every record is initialized in that way, regardless whether it contains fixed array or not. For records which don't have fixed sized arrays the CreateRecord method simply looks like:



public struct TRecord



  public static TRecord CreateRecord(){return new TRecord();}




However, if a record type is replaced by another one by refactoring, the initialization is done by "new":


Guid result = new Guid();



This page belongs to the Delphi2C# Documentation

Delphi2C# home  Content