|
| Override property type in descendant class |
 |
6 Mar 2008 07:49:32 -0700 |
I have created 2 top level classes that I plan to use for a list and a list
item.
Simplified version:
TListItem = Class
End;
TList = Class
Private
FFirst : TListItem;
Protected
Function GetFirst : TListItem; Virtual;
Procedure SetFirst (AValue : TListItem); Virtual;
Public
Property First : TListItem Read GetFirst Write SetFirst;
End;
I would now like to sub-class from these classes to make specific list types.
For example, a Integer list.
First, I subclass TListItem like this to add a Data property of type Integer to
my list item:
TIntegerListItem = Class(TListItem)
Private
FData : Integer;
Protected
Function GetData : Integer; Virtual;
Procedure SetData (AValue : Integer); Virtual;
Public
Property Data : Integer Read GetData Write SetData;
End;
I can now use my TList and TIntegerListItem together, however, anytime, I want
to access the Data property of the First item, I have to type cast or do an AS
like this:
(MyList.First As TIntegerListItem).Data
-- OR --
TIntegerListItem(MyList.First).Data
I want to create a new list type called TIntegerList that will hold
TIntegerListItem objects. That way, when the I access the list items through
the list, I don't have to constantly typecast. It would be ideal if I could do
something like this:
TIntegerList = Class (TList)
Public
Property First : TStringListItem;
End;
That way, I'd only have to list the property with a new type rather than
re-implementing all the Read Write methods, etc.
Any suggestions on the best way to accomplish my goal here?
Josh Breitbach
|
| Post Reply
|
| Re: Override property type in descendant class |
 |
6 Mar 2008 09:06:37 -0700 |
That's what I was afraid of. So there's no way to do it without re-defining the
interface and implementation of all those methods?
That's unfortunate.
Thanks,
Josh Breitbach
http://www.breittechnologies.com
|
| Post Reply
|
| Re: Override property type in descendant class |
 |
6 Mar 2008 11:09:56 -0700 |
Josh Breitbach wrote:
>
> I have created 2 top level classes that I plan to use for a list and
> a list item.
>
> Simplified version:
>
> TListItem = Class
> End;
>
> TList = Class
> Private
> FFirst : TListItem;
> Protected
> Function GetFirst : TListItem; Virtual;
> Procedure SetFirst (AValue : TListItem); Virtual;
> Public
> Property First : TListItem Read GetFirst Write SetFirst;
> End;
>
>
> I would now like to sub-class from these classes to make specific
> list types. For example, a Integer list.
>
> First, I subclass TListItem like this to add a Data property of type
> Integer to my list item:
>
> TIntegerListItem = Class(TListItem)
> Private
> FData : Integer;
> Protected
> Function GetData : Integer; Virtual;
> Procedure SetData (AValue : Integer); Virtual;
> Public
> Property Data : Integer Read GetData Write SetData;
> End;
>
>
> I can now use my TList and TIntegerListItem together, however,
> anytime, I want to access the Data property of the First item, I have
> to type cast or do an AS like this:
>
> (MyList.First As TIntegerListItem).Data
> -- OR --
> TIntegerListItem(MyList.First).Data
>
> I want to create a new list type called TIntegerList that will hold
> TIntegerListItem objects. That way, when the I access the list items
> through the list, I don't have to constantly typecast. It would be
> ideal if I could do something like this:
>
> TIntegerList = Class (TList)
> Public
> Property First : TStringListItem;
> End;
>
> That way, I'd only have to list the property with a new type rather
> than re-implementing all the Read Write methods, etc.
>
> Any suggestions on the best way to accomplish my goal here?
>
As long as we don't have generics in Win32 you either have to use a
code generator like
Typed list generator for Delphi
http://cc.codegear.com/item/24490
or a template system like http://dn.codegear.com/article/27603
The problem cannot be solved using inheritance.
--
Peter Below (TeamB)
Don't be a vampire (http://slash7.com/pages/vampires),
use the newsgroup archives :
http://www.tamaracka.com/search.htm
|
| Post Reply
|
| Re: Override property type in descendant class |
 |
Thu, 06 Mar 2008 14:37:11 -030 |
You must inherit TList and override GetFirst method using something like
this:
// in the interface section of your descendant TList
...
property First: TIntegerListItem read GetFirst write SetFirst;
...
// implementation of inherited GetFirst
function TNewList.GetFirst: TIntegerListItem;
begin
Result := TIntegerListItem(inherited GetFirst);
end;
Leonardo M. Ramé
http://leonardorame.blogspot.com
Josh Breitbach escribió:
> I have created 2 top level classes that I plan to use for a list and a list
item.
>
> Simplified version:
>
> TListItem = Class
> End;
>
> TList = Class
> Private
> FFirst : TListItem;
> Protected
> Function GetFirst : TListItem; Virtual;
> Procedure SetFirst (AValue : TListItem); Virtual;
> Public
> Property First : TListItem Read GetFirst Write SetFirst;
> End;
>
>
> I would now like to sub-class from these classes to make specific list
types. For example, a Integer list.
>
> First, I subclass TListItem like this to add a Data property of type
Integer to my list item:
>
> TIntegerListItem = Class(TListItem)
> Private
> FData : Integer;
> Protected
> Function GetData : Integer; Virtual;
> Procedure SetData (AValue : Integer); Virtual;
> Public
> Property Data : Integer Read GetData Write SetData;
> End;
>
>
> I can now use my TList and TIntegerListItem together, however, anytime, I
want to access the Data property of the First item, I have to type cast or do an
AS like this:
>
> (MyList.First As TIntegerListItem).Data
> -- OR --
> TIntegerListItem(MyList.First).Data
>
> I want to create a new list type called TIntegerList that will hold
TIntegerListItem objects. That way, when the I access the list items through
the list, I don't have to constantly typecast. It would be ideal if I could do
something like this:
>
> TIntegerList = Class (TList)
> Public
> Property First : TStringListItem;
> End;
>
> That way, I'd only have to list the property with a new type rather than
re-implementing all the Read Write methods, etc.
>
> Any suggestions on the best way to accomplish my goal here?
>
> Josh Breitbach
|
| Post Reply
|
| Re: Override property type in descendant class |
 |
Thu, 6 Mar 2008 18:08:49 -0500 |
"Josh Breitbach" <josh@breittechnologies.com> wrote in message
news:47d0249d$1@newsgroups.borland.com...
>
> That's what I was afraid of. So there's no way to do it without
> re-defining the interface and implementation of all those methods?
>
> That's unfortunate.
>
I just recently worked through the same issue. Your problem may be an
indication of a design flaw. The properties of an object make the object and
if you're having problems with that you may need to rethink whether or not
that property should be in the base class. Also, you may find (as I did)
that when you go to use the objects you won't need to have a generic
property in the base class.
|
| Post Reply
|
|
|
|
|
|
|
|
|
|