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 9082345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:32:14+00:00 2026-06-16T20:32:14+00:00

i have ObjectList container and i want to add an internal iterator (Visitor Pattern)

  • 0

i have ObjectList container and i want to add an internal
iterator (Visitor Pattern) in fact i’m attempting to determine
duplicates in my List..

a sample: http://pastebin.com/pjeWq2uN

this code to provide an insight of what i’m trying to achieve..

TFindDuplicatesMethod = procedure(s1, s2: string) of object;

TPersonList = class(TObjectList)
public
  procedure Iterate(pMethode: TFindDuplicatesMethod)
end;

procedure TPersonList.Iterate(pMethode: TFindDuplicatesMethod)
var
  i: Integer;
begin
  for i := 0 to Count - 1 do
  pMethode(TMyClass(Items[i]).S1, {But i don't have the second parameter because
                               it comes from outside of PersonList Ex: OpenDialog.Files[i]})
end;

function FindDuplicate(S1, S2: string): Boolean;
begin
  Result := False;
  if S1 = S2 then
  Result := True;
end;

begin
  Files.Iterate(FindDuplicates(S1, S2));
end;

i’m wondering how OOP solve such problem.

thank’s 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-06-16T20:32:15+00:00Added an answer on June 16, 2026 at 8:32 pm

    Ok, as we found in comments, we have 2 tasks:

    1. How to find if TObjectList already contains an item (so new item is an duplicate)
    2. How to manage file icons in TImageList to reduce memory usage and store only unique icons.

    As I mentioned in comments, you should ask about your second question in separate thread, but I suggest you to add new files icons depeding on new file mime-type, instead of binary icon data. Here you have to create file-type dictionary, determine file-type and so on..

    What about duplicates in TObjectList.
    You probably know, that there is generic implemntation of TObjectList – TObjectsList<T>. As in your example you can define TPersonList as TObjectList<TPerson>, so items property always returns TPerson objects instance.

    now, generic task with lists – list sorting. Take a look at Sort() method of TObjectList<T>/TList. It has 2 overload methods. One of them is default, and second takes an Comparer as parameter. Actually, the first method also uses an comparer – default comparer.
    So comparer is an implemntation of IComparer<T> interface wich has the only method – function Compare(l,r : T):integer; Usually you define this sort-comparer at runtime as anonimous method, before calling the Sort() method. Using you anonimous method you always know how to compare two T-typed objects, and then you can determine wich of them is “greater” than other, and should be the first in list.

    so the same situation you have while searching for duplicates in list. but now you have to determine, are 2 objects equal or not.

    Let us suppose you have personList : TPersonList wich contains TPerson instances. Each person has, for exmaple, name, surname, date of birth and ID.
    Of course default comparer knows nothing about how to compare 2 persons. But you can provide new comparer wich knows. For example, let suppose 2 objects are equals, if their IDs are equal;

        TPerson = class(TObject)
          strict private
            FName : string;
            FSurname : string;
            FDateOfBirth : TDateTime;
            FId : string;   {passport number or something else}
          public
            constructor Create(aID : string; aDoB : TDateTime);
    
            property Name : string read FName write FName;
            property Surname : string read FSurname write FSurname;
            property DateOfBirth : TDateTime read FDateOfBirth;
            property ID : string read FId;
        end;
    
    
        TPersonList = class(TObjectList<TPerson>)
          public
            constructor Create();
        end;
    

    TPerson constructor is usual:

    constructor TPerson.Create(aID: string; aDoB: TDateTime);
    begin
        inherited Create();
        FID := aId;
        FDateOfBirth := aDoB;
    end;
    

    now we have to write TPersonList contructor. As you can see,TObejctList constructor has few overloads. One of them has Comparer parameter. It saves aComparer to FComparer field. Now, take a look at Contains method. It finds does list already contain object or not. It uses IndexOf method. So if returned index = 0 then list contains our object.

    So now our task is to define new comparer in TPersonList constructor. We should define comparsion method, then create comparer object and pass it to List contructor.

    constructor TPersonList.Create();
    var comparer : IComparer<TPerson>;
        comparison : TComparison<TPerson>;
    begin
        comparison := function(const l,r : TPerson):integer
                      begin
                        if l.ID = r.id then exit(0)
                        else if l.ID > r.ID then exit(-1)
                        else exit(1);
                      end;
    
        comparer := TComparer<TPerson>.Construct(comparison);
    
        inherited Create(comparer);
    end;
    

    to test our code, lets add some persons to list.

    procedure TForm2.FormCreate(Sender: TObject);
    var persons : TPersonList;
    
        function AddPerson(id : string; date : TDateTime):boolean;
        var p : TPerson;
        begin
            p := TPerson.Create(id, date);
    
            result := not persons.Contains(p);
    
            if result then
                persons.Add(p)
            else begin
                ShowMessage('list already contains person with id = ' + id);
                p.Free();
            end;
        end;
    begin
        persons := TPersonList.Create();
        try
            AddPerson('1', StrToDate('01.01.2000'));
            AddPerson('2', StrToDate('01.01.2000'));
            AddPerson('3', StrToDate('01.01.2000'));
            AddPerson('2', StrToDate('01.01.2000')); // we will get message here.
        finally
            persons.Free();
        end;
    end;
    

    So, this is the usual way how to determine if TList (or its descendant) contains object.

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

Sidebar

Related Questions

Assuming I have object_list list which contains objects. I want to check if my
If I have a list of object: var objectList= LIST_OF_OBJECT; each object in the
I have a list of objects and I want to sort by passing the
I have an object model MyObject that contains a list of long called ObjectList
I have a list of objects that I want to filter by an integer
I have an object list being loded from a database to a drop down
I have a list object List<Documents> , and inside that I have another list
I have class A with a public std::list<int> object list_ . Class B ,
I am attempting to read a text file into a linear linked list of
I have a problem with list refresh when user closes one activity and returns

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.