It's been a while now, but this post continues the series of The Delphi Language Chapter teasers from Jon Shemitz’ .NET 2.0 for Delphi Programmers book.
What a good time to continue this series just after the major Delphi for .NET 2.0 launch, CodeGear RAD Studio 2007!
The previous post covered the exotic topic of Record Helpers. Today we'll look at another exotic-ish topic - overloaded default array properties.
Note that I do not get any royalties from the book and I highly recommend that you get your own copy – for instance at Amazon.
"Overloaded default array properties
A class or component can have an array property that is declared as default. This mechanism has now been extended to allow multiple overloaded default array properties - as long as the number or types of indexer parameters are different.
type
TMyObject = class
public
property Items[Index: integer]: string read GetItems write SetItems; default;
property Items[const Name: string]: string read GetNamedItems write SetNamedItems; default;
end;
This means that you can use the array indexing syntax on the object instance - effectively overloading the array subscript operator [].
procedure Test;
var
MyObject: TMyObject;
begin
MyObject := TMyObject.Create;
MyObject[42] := 'The Answer';
MyObject['Bar'] := 'Yes';
end;
"
Is this working for both Win32 and .NET or only for .NET?
ReplyDeleteIt works in Delphi 2007 Win32 R2, haven't tested .Net though.
ReplyDeleteCode completion doesn't seem to understand it but who cares! This owns, I've wanted this several times over the last few years. :)
It also works in D2006.
ReplyDeleteBut indeed, you have to trick codecompletion a bit
In general, the Delphi Language chapter in .Net for Delphi Programmers targets the lanugage supported in Delphi 2006.
ReplyDeleteUnless explicitly noted, the language features work in both Win32 and .NET.
Some years later... there's the issue of using overloaded array properties with BCB:
ReplyDeletehttps://quality.embarcadero.com/browse/RSP-14999