 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Lee Parker Guest
|
Posted: Tue Nov 04, 2003 4:35 pm Post subject: Where can I find a SIMPLE example of... |
|
|
I'm doing some examples comparing linked lists and arrays...
I want to do something like:
type
TMyClass = class(TObject)
protected
fDesc :String;
fCreated :TDateTime;
published
property Description :string read fDesc write fDesc;
property Created :TDateTime read fCreated write FCreated;
end;
TMyClassList = class(TObject)
fList :Array Of TMyClass;
private
function GetIt(Index :Integer) :TMyClass;
procedure SetIt(Index :Integer; Value :TMyClass);
public
function Count :Integer;
property Item[Index :Integer] :TMyClass read GetIt write SetIt;
end;
My question is: Since this would be a poor implementation, is there a
SIMPLE example of the above and if so, where can I find it?
Thanks,
Lee
|
|
| Back to top |
|
 |
Kurt Barthelmess Guest
|
Posted: Tue Nov 04, 2003 7:47 pm Post subject: Re: Where can I find a SIMPLE example of... |
|
|
"Lee Parker" <lparker (AT) mountville (DOT) com> wrote:
| Quote: | I'm doing some examples comparing linked lists and arrays...
I want to do something like:
[clip] |
ok, but what does that have to do with "comparing linked lists and
arrays"?
| Quote: | My question is: Since this would be a poor implementation, is there a
SIMPLE example of the above and if so, where can I find it?
|
I don't see anything "poor" with the implementation. I might have used
a record instead of an object for TMyClass, but there's probably a
whole bunch of other "smart" code not shown that would make that a bad
choice.
fwiw, I just looked at a unit I used to implement a "sparse" doubly
dimensioned array of items similar to those you are managing. It took
200+ lines to implement, so I'm not sure there is such a thing as a
"simple" example.
Good luck.
Kurt
|
|
| Back to top |
|
 |
John Leavey Guest
|
Posted: Wed Nov 05, 2003 11:55 am Post subject: Re: Where can I find a SIMPLE example of... |
|
|
On 4 Nov 2003 09:35:44 -0700, "Lee Parker" <lparker (AT) mountville (DOT) com> wrote:
| Quote: |
I'm doing some examples comparing linked lists and arrays...
I want to do something like:
type
TMyClass = class(TObject)
protected
fDesc :String;
fCreated :TDateTime;
published
property Description :string read fDesc write fDesc;
property Created :TDateTime read fCreated write FCreated;
end;
TMyClassList = class(TObject)
fList :Array Of TMyClass;
private
function GetIt(Index :Integer) :TMyClass;
procedure SetIt(Index :Integer; Value :TMyClass);
public
function Count :Integer;
property Item[Index :Integer] :TMyClass read GetIt write SetIt;
end;
My question is: Since this would be a poor implementation, is there a
SIMPLE example of the above and if so, where can I find it?
|
You could base your list class on TList, just wrapping the various methods to make them
type safe. TList does use an internal array to hold the data, but is more efficient in how
it resizes the array when items are added - see TList.Grow in Classes.Pas.
e.g.
TMyItemList = Class( TList )
protected
procedure Put( Index: Integer; Item: TMyItem);
function Get( Index: Integer ): TMyItem;
public
property Items[ Index: Integer ]: TMyItemread Get write Put; Default;
procedure Add( Item: TMyItem);
end;
procedure TMyItemList .Add( Item: TMyItem);
begin
inherited Add( Item );
end;
function TMyItemList .Get( Index: Integer ): TMyItem;
begin
Result := inherited Get( Index );
end;
procedure TMyItemList .Put( Index: Integer; Item: TMyItem);
begin
inherited Put( Index, Item );
end;
You could then add your own methods to clear the items in the list when it is destroyed
(if the list owns the items it contains) or to provide custom sorting.
For a utility to generate code for a more complete type safe list, look at
http://www.delphi32.com/vcl/4435/
John Leavey
|
|
| Back to top |
|
 |
Raoul Snyman Guest
|
Posted: Sat Nov 08, 2003 6:07 pm Post subject: Re: Where can I find a SIMPLE example of... |
|
|
Lee Parker wrote:
| Quote: | I'm doing some examples comparing linked lists and arrays...
snip
My question is: Since this would be a poor implementation, is there a
SIMPLE example of the above and if so, where can I find it?
snip |
you could also create a TStrings or TStringList object, and use it's
Objects property in conjunction with AddObject() and other functions. if
you're using classes or objects of course.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|