[fpc-pascal] random question

Vladimir Zhirov vvzh.lists at gmail.com
Fri Jun 4 18:57:25 CEST 2010


spir wrote:

> -2- [] operator
> How to implement a class that manages this operator (did not find it
> in the operator overloading section). Pointer welcome (including to
> the implementation of eg TFPList).

It seems default properties is what you are looking for.

>> type 
>> TMyClass = class
>> ...
>> property Items[AnIndex: Integer]: TMyItem read GetItem write SetItem; default;

Note the "default" keyword, it allows you to access Items like this:

>> MyClassInstance[1]

instead of

>> MyClassInstance.Items[1]

You can also use string instead of Integer index, then accessing
such property would look like dealing with associative arrays:

>> MyClassInstance['first']

or 

>> MyClassInstance.Items['first']

GetItem and SetItem methods must take the appropriate parameters,
and return the appropriate result. You can let Lazarus generate
them for you by declaring the property and pressing Ctrl+C while
cursor is on the same line with your property declaration.

-- 
Regards,
Vladimir Zhirov



More information about the fpc-pascal mailing list