Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7400819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:18:25+00:00 2026-05-29T04:18:25+00:00

Currently I am adding object by creating it like: type TRecord = class private

  • 0

Currently I am adding object by creating it like:

type    
  TRecord = class
  private
    str: string;
    num: Integer;
  public
    constructor Create;
  end;

...

procedure TForm1.Button2Click(Sender: TObject);
var
  i: Integer;
  rec: TRecord;
  Alist: TStringList;
begin
  Alist := TStringList.create;
  Alist.Clear;
  for i := 0 to 9 do 
  begin
    rec := Trecord.Create; //create instance of class
    rec.str := 'rec' + IntToStr(i);
    rec.num := i * 2;
    Alist.AddObject(IntToStr(i), rec);
  end;
end;

Is this method correct or inefficient ?
Or Can I directly add object not by creating it like using record?

type    
  PRec = ^TRec;
  TRec = record
    str: string;
    num: Integer;
  end;

...
var
  rec: TRec;
...

for i := 0 to 9 do 
begin
  //how to write here to have a new record, 
  //can i directly Create record in delphi 7 ?
  rec.str := 'rec' + IntToStr(i);
  rec.num := i*2;
  Alist.AddObject(IntToStr(i), ???); // how to write here?
end;

Or other fast and simple way?

I am using Delphi 7.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:18:26+00:00Added an answer on May 29, 2026 at 4:18 am

    The way you’re doing it now is fine.

    You can’t do it with a record without allocating memory when you add a new record to the TStringList.Objects, and you’d have to free it afterwards. You’re just as well off using a class as you are now; you have to free the objects before freeing the stringlist. (In more recent versions of Delphi, TStringList has an OwnsObjects property that will auto-free them for you when the stringlist is free’d, but it’s not in Delphi 7.)

    If you really want to do this with a record, you can:

    type    
      PRec = ^TRec;
      TRec = record
        str: string;
        num: Integer;
      end;
    
    var
      rec: PRec;
    begin
      for i := 0 to 9 do 
      begin
        System.New(Rec);
        rec.str := 'rec' + IntToStr(i);
        rec.num := i*2;
        Alist.AddObject(IntToStr(i), TObject(Rec)); // how to write here?
      end;
    end;
    

    You’ll need to use System.Dispose(PRec(AList.Objects[i])) to release the memory before freeing the stringlist. As I said, the way you’re doing it now is actually much easier; you don’t have to do the typecast when adding to and deleting from the stringlist.

    You don’t need the AList.Clear, by the way. Since you’re creating the stringlist, there can’t be anything in it to remove.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code I'm currently working on requires adding an NSNumber object to an array.
I'm currently adding verbose tooltips to our site, and I'd like (without having to
I'm currently adding some new extended classes to this code: foreach (BaseType b in
I'm currently adding a div to use as a slider programmatically, but when I
I currently am adding some features to our logging-library. One of these is the
I'm adding a toolbar to my application and currently I'm adding some toggle buttons
I'm currently evaluating options for adding sub-domain support to a new Ruby on Rails
Currently I have a loop that tries to find an unused filename by adding
I'm currently in the process of adding JNI functionality into a legacy delphi app.
I'm currently running a Rails migration where I am adding a datatype specific to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.