Mattias Gaertner wrote:
On Fri, 16 Jun 2006 08:03:19 +0200
Micha Nelissen <[EMAIL PROTECTED]> wrote:
Tony Pelton wrote:
Classes with indexed properties work and look like associative arrays.
how do index'ed properties help you store objects by a key ?
Something like:
type
THash = class(TObject)
protected
function GetItem(AKey: string): string;
procedure SetItem(AKey, NewValue): string;
public
property Items[AKey: string]: string read GetItem
write SetItem default;
end;
The 'default' keyword makes the array usable "on the instance":
var
Hash: THash;
begin
Hash := ...
Hash['sun'] := 'yellow';
Hash['sky'] := 'blue';
{ etc ... }
end.
See lcl/stringhashlist.pas
For numeric keys (or keys upto 32 bytes) you also can use TMap
(lcl/maps.pp). The key (=ID) can be signed or unsigned value of 1 to 32
bytes long. The value (=data) can be of any size.
var
Map: TMap;
begin
Map := TMap.Create(itu4, SizeOf(TObject));
Map.Add(1, myobject);
Map.Add(123456, myotherobject);
...
end.
Marc
_________________________________________________________________
To unsubscribe: mail [EMAIL PROTECTED] with
"unsubscribe" as the Subject
archives at http://www.lazarus.freepascal.org/mailarchives