Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Specialized TList 1

Status
Not open for further replies.

Glenn9999

Programmer
Jun 19, 2004
2,311
US
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;

 
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...
 
Still using my copy of Delphi 3 like I've always been...

 
Oh boy! ;)

-----------------------------------------------------
Helping people is my job...
 
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.

 
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.
 
If people really want to know, the D3 (a higher-end version) has been taking care of all of my needs as a hobbyist programmer (meaning I don't work for a company, but I do turn out stuff and distribute it online), for most part. I've had to lean on a copy of Turbo Delphi 2006 for some calls that involve 64-bit elements and have evaluated a couple of newer versions, including Delphi Starter Tokyo. Unfortunately the license on that one is so restrictive I can't do a whole lot with it, including release stuff or do any custom controls (from what I understand of the license). I notice the newer the Delphi version gets the more clunky, slow, problematic and bug-ridden it gets. Not to mention, as what I do is not money-making by any stretch, it's hard to justify the $3514 price (to get the equivalent of the D3 I have here). I've tried Lazarus too, it's such a different language from Delphi that none of my stuff compiles. If I have to learn a new language, I might as well jump to Visual Basic or Visual C++ as those compilers are offered on a lot more favorable basis than Delphi. I have enough unfinished stuff and stuff I can potentially maintain that I haven't particularly had the desire to learn another language and rewrite all the stuff I've done ever since I bought the D3 in the first place.

In short, there's no benefit for me to upgrade. So I'm not.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top