Lately I've been thinking of posting more to the FAQ section - if it's even being read these days. One thing I've been considering posting is a description of descending TList to handle specialized data types (e.g. like TStringList). But I had a couple of questions before I would post it. (Sample of the code I mean to post below)
1. I'm not sure whether I got the Delete method right, so it won't cause memory leaks. Any thoughts?
2. Is there a better way to format this to be able to assign data? What I thought I had worked, but things like IncData occur because the compiler produces an error "Left side can not be assigned to." Is there a good way to get clean access to each part of a data block like TDataRec without making methods to do it?
1. I'm not sure whether I got the Delete method right, so it won't cause memory leaks. Any thoughts?
2. Is there a better way to format this to be able to assign data? What I thought I had worked, but things like IncData occur because the compiler produces an error "Left side can not be assigned to." Is there a good way to get clean access to each part of a data block like TDataRec without making methods to do it?
Code:
TDataRec = record
DataNum: Integer;
Data: String;
end;
PDataRec = ^TDataRec;
TDataList = class(TList)
private
RecType: PDataRec;
public
procedure Add(Datanum: integer; inData: string);
function Delete(Index: Integer): Boolean;
procedure IncData(index: Integer);
procedure Clear;
protected
function Get_Data(Index: Integer): TDataRec;
procedure Set_Data(Index: Integer; const Data: TDataRec);
published
property Data[Index: Integer]: TDataRec read Get_Data write Set_Data;
end;
function TDataList.Get_Data(Index: Integer): TDataRec;
begin
Result := TDataRec(Items[Index]^);
end;
procedure TDataList.Set_Data(Index: Integer; const Data: TDataRec);
begin
TDataRec(Items[Index]^) := Data;
end;
procedure TDataList.Clear;
var
i: integer;
begin
for i := (Count - 1) downto 0 do
if Items[i] <> nil then
Dispose(Items[i]);
inherited Clear;
end;
procedure TDataList.Add(Datanum: integer; inData: string);
// adds an Data to the count list.
var
Rec: TDataRec;
begin
Rec.Data := inData;
Rec.DataNum := Datanum;
New(RecType);
RecType^ := Rec;
inherited Add(RecType);
end;
function TDataList.Delete(Index: Integer): Boolean;
begin
Result := true;
try
RecType := Items[Index];
inherited Delete(Index);
// shouldn't try this if not successful?
if RecType <> nil then
dispose(RecType);
except
Result := false;
end;
end;
function TDataList.FindData(inData: string): Integer;
var
i: integer;
begin
Result := -1;
i := 0;
while i <= (Count - 1) do
begin
if Data[i].Data = inData then
begin
Result := i;
break;
end;
inc(i);
end;
end;
function TDataList.FindDataIndex(IndexNum: Integer): Integer;
// locates index of rebus in List based on the Index/Datanum
var
i: integer;
begin
Result := -1;
i := 0;
while i <= (Count - 1) do
begin
if Data[i].DataNum = IndexNum then
begin
Result := i;
break;
end;
inc(i);
end;
end;
procedure TDataList.IncData(index: Integer);
begin
Inc(TDataRec(Items[Index]^).DataNum);
end;