Title: Create Your Own List Of Objects
Question: Any experienced programmer knows how boring it is to write lists of objects, because they are so very alike. Only the Object contained in the list differs.
That's why I made this object. It is a descendant of the general purpose TList, and can easily be modifyed to contain any object.
First I will present the object. Later I will describe the functions in detail.
Answer:
unit MyObject;
interface
uses
Classes;
type
TMyObject = class
private
fValue1 : string;
fValue2 : string;
public
constructor Create( AValue1, AValue2 : string );
property Value1 : string read fValue1 write fValue1;
property Value2 : string read fValue2 write fValue2;
end;
TMyObjectList = class( TList )
private
function GetItem( AIndex : integer ) : TMyObject;
public
constructor Create;
destructor Destroy; override;
function Add( AMyObject : TMyObject ) : integer;
function IndexOf( AMyObject : TMyObject ) : integer;
procedure Clear;
procedure Insert( AIndex : integer; AMyObject : TMyObject );
procedure Delete( AIndex : integer );
procedure Remove( AMyObject : TMyObject );
property Items[ AIndex : integer ] : TMyObject read GetItem;
end;
{ ------------------------------------------------------------------ }
implementation
{ ------------------------------------------------------------------ }
constructor TMyObject.Create( AValue1, AValue2 : string );
begin
fValue1 := AValue1;
fValue2 := AValue2;
end;
{ ------------------------------------------------------------------ }
{ ------------------------------------------------------------------ }
constructor TMyObjectList.Create;
begin
inherited Create;
end;
{ ------------------------------------------------------------------ }
destructor TMyObjectList.Destroy;
begin
try
Clear;
finally
inherited Destroy;
end;
end;
{ ------------------------------------------------------------------ }
function TMyObjectList.Add( AMyObject : TMyObject ) : integer;
begin
result := inherited Add( AMyObject );
end;
{ ------------------------------------------------------------------ }
function TMyObjectList.IndexOf( AMyObject : TMyObject ) : integer;
begin
result := inherited IndexOf( AMyObject );
end;
{ ------------------------------------------------------------------ }
procedure TMyObjectList.Insert( AIndex : integer; AMyObject : TMyObject );
begin
inherited Insert( AIndex, AMyObject );
end;
{ ------------------------------------------------------------------ }
procedure TMyObjectList.Delete( AIndex : integer );
begin
Items[ AIndex ].Free;
inherited Delete( AIndex );
end;
{ ------------------------------------------------------------------ }
procedure TMyObjectList.Remove( AMyObject : TMyObject );
begin
Items[ IndexOf( AMyObject ) ].Free;
inherited Remove( AMyObject );
end;
{ ------------------------------------------------------------------ }
function TMyObjectList.GetItem( AIndex : integer ) : TMyObject;
begin
result := TMyObject( inherited Items[ AIndex ] );
end;
{ ------------------------------------------------------------------ }
procedure TMyObjectList.Clear;
begin
while inherited Count 0 do
Delete( 0 );
end;
end.
TMyObject = class
This is the object contained in the list. This example object contains 2 strings.
Exchange this object with your own when writing your own program.
constructor TMyObjectList.Create;
This function overrides the TList.Create.
var
MyList : TMyObjectList;
begin
MyList := TMyObjectList.Create;
end;
destructor TMyObjectList.Destroy override;
This function overrides the TList.Destroy and frees all objects in the list before destroying itself.
The TList.Destroy only desttoys the list itself, not the objects contained in the list. This is why we need this function.
Never call Destroy directly. Always use TMyObjectList.Free:
MyList.Free;
function TMyObjectList.Add( AMyObject : TMyObject ) : integer;
Use this function to add objects to the list:
MyList.Add( TMyObject.Create( 'VALUE1', 'VALUE2' ) );
function TMyObjectList.IndexOf( AMyObject : TMyObject ) : integer;
This function returns the index of the object in the list.
variable := MyList.IndexOf( MyObject );
procedure TMyObjectList.Clear;
This function overrides the TList.Clear, because that function only clears the list of pointes, not the objects in the list.
MyList.Clear;
procedure TMyObjectList.Insert( AIndex : integer; AMyObject : TMyObject );
This function is used to insert an object on a predefined position in the list.
// Inserting a new object on position 5
MyList.Insert( 5, TMyObject.Create( 'VALUE1', 'VALUE2' ) );
procedure TMyObjectList.Delete( AIndex : integer );
This function overrides the TList.Delete because that function only deleted the index in the list not the object on that index.
// Deleting first entry of the list, including object
MyList.Delete( 0 );
procedure TMyObjectList.Remove( AMyObject : TMyObject );
This function removed object and entry in the list. Use this function when the object is known but not the position in the list.
MyList.Remove( MyObject );
property Items[ AIndex : integer ] : TMyObject read GetItem;
This is pure Delphi magic. The property Items is actually a function called GetItem that returns the object from a index in the list.
The TList.Items only returns a pointer to an object, so that is why we need to override it in order to return the object itself.
var
i : integer;
begin
// This function adds all items from the
// MyList to a TListBox called ListBox1
for i := 0 to MyList.Count-1 do
ListBox1.Items.AddObject( MyList.Items[i].Value1 +
MyList.Items[i].Value2,
MyList.Items[i] );
end;
TList Descendants
You can still use all public functions and properties from the TList such as:
MyList.Count; // Number of objects in list
MyList.Pack; // Pack List
// ...And so forth. Look in the Delphi on-line help
// for a copmplete list of functions