DSN94
DSN94

Reputation: 304

Property of an Array of Integers

i'm starting with Object-Pascal in Delphi, but i'm getting stuck in using basic properties when they access arrays. I know that simple arrays may not be the best way of dealing with sets of data, but i want to get the basics first, and i can't find any documentation that covers this topic at a basic level

So here is the interface 'code' :

 TExample = class(TObject)
     private
         intArray : array of integer;
         procedure setIntArray(value : integer);
         function getIntArray(index : integer);
     published
         //This is where my problem is
         //E2008 Incompatible types
         property PIntArray : Integer read getIntArray write setIntArray;

         //I tried this way, too
         //Property PIntArray cannot be of type ARRAY
         property PIntArray[Index : Integer] : Integer read getIntArray write setIntArray;
 end;

So, how could i fix this?

Thank you for your time.

Upvotes: 1

Views: 3639

Answers (1)

David Heffernan
David Heffernan

Reputation: 613252

You have a variety of options. Perhaps the most commonly used is an array property. Like this:

type
  TExample = class(TObject)
  private
    FItems: TArray<Integer>;
    function GetItem(Index: Integer): Integer;
    procedure SetItem(Index: Integer; Value: Integer);
  public
    property Items[Index: Integer]: Integer read GetItem write SetItem;
  end;

function TExample.GetItem(Index: Integer): Integer;
begin
  Result := FItems[Index];
end;

procedure TExample.SetItem(Index, Value: Integer);
begin
  FItems[Index] := Value;
end;

Here the getter and setter read and write individual items from the array. You'd obviously also need a Count property to make this type useful.

I'm not sure where you looked for documentation, but it can be found here: http://docwiki.embarcadero.com/RADStudio/en/Properties#Array_Properties

Upvotes: 4

Related Questions