×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Specialized TList

Specialized TList

Specialized TList

(OP)
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?

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; 

RE: Specialized TList

I fail to see why not you wouldn't use TList<T> (unless this is targeted at an ancient Delphi version)?

-----------------------------------------------------
Helping people is my job...

RE: Specialized TList

(OP)
Still using my copy of Delphi 3 like I've always been...

RE: Specialized TList

Oh boy! ;)

-----------------------------------------------------
Helping people is my job...

RE: Specialized TList

(OP)
Still works for what I want to do with it. BTW, did a rewrite and got it down to 10 lines, plus record definitions, outside of my base class. So I got it figured out.

RE: Specialized TList

I used D6 for many years, and then bit the bullet and spent a couple of years going through XE4 to XE8 before giving up on the upgrade costs.

I still use D6 for some old projects that run on even older OSes, and I run it in a VM on WinXP so the projects don't get mixed up, etc. It's always a bit of a shock when going back to it for how, mrm, clunky?, lacking? it feels. Particularly generics.

You're right - it's still perfectly capable, but the new versions have a lot of compelling features, and language and library upgrades.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close