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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:07:02+00:00 2026-05-18T01:07:02+00:00

I knew how to do this but forgot again… Quite irritating, because I’m working

  • 0

I knew how to do this but forgot again… Quite irritating, because I’m working on a class that contains a list of XML files, and now I just want to use a for-in loop to walk through all files in this list. This is the class I have right now:

type
  TXmlFileList = class( TInterfacedObject )
  private
    type
      TItem = class( TInterfacedObject )
      strict private
        FCaption: string;
      protected
        constructor Create( const ACaption: string; const AXML: WideString );
      public
        destructor Destroy; override;
        property Caption: string read FCaption;
      end;
  strict private
    FXmlFiles: array of TXmlFileList.TItem;
  strict protected
    function GetXmlFile( index: Integer ): TXmlFileList.TItem;
  public
    constructor Create( );
    destructor Destroy; override;
    function Add( const ACaption: string; const AXML: WideString ): Integer; overload;
    function Add( const AFilename: string ): Integer; overload;
    function Count: Integer;
    procedure Clear;
    property XmlFile[ index: Integer ]: TXmlFileList.TItem read GetXmlFile; default;
  end;

Looks funny? 🙂 I know, but I want to hide the definition of the TXmlFile class to the outside world. Basically, the TXmlFileList class allows me to simply refer to XmlFileList[I] to get the file at position I. Works nicely.

But now I want to loop through the TXmlFileList.TItem elements, so I have to expose the TXmlFileList.TItem class. It’s not enough, though. It needs an enumerator too in the TXmlFileList class!
How to create that enumerator?

You’re probably wondering why I use this complex construction. Well, it might be complex but it will be used by some other developers and I don’t want to provide more methods than they need. This way, I only give them the methods “Add”, “Clear” and “Count” to loop through the list, and any property defined on the TItem itself. They don’t need more than this, although I might add a few more features later on…

  • 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-18T01:07:02+00:00Added an answer on May 18, 2026 at 1:07 am

    Found it! I needed to create a new class, which I’ve called TItemEnumerator, and I also included it in the TXmlFileList class, right after TItem:

        type
          TItemEnumerator = class( TObject )
          strict private
            FOwner: TXmlFileList;
            FIndex: Integer;
          protected
            constructor Create(AOwner: TXmlFileList);
          public
            function GetCurrent: TItem;
            function MoveNext: Boolean;
            property Current: TItem read GetCurrent;
          end;
    

    Implementation was easy, just an additional method to TXmlFileList:

        function GetEnumerator: TItemEnumerator;
    

    And finally, exposing the class to the outside world, which I did by adding this to TXmlFileList:

    type TXmlFile = TXmlFileList.TItem;
    

    Yeah, that dirty! 🙂


    It results in this code:

    type
      TXmlFileList = class( TInterfacedObject )
      private
        type
          TItem = class( TInterfacedObject )
          strict private
            FCaption: string;
          protected
            constructor Create( const ACaption: string; const AXML: WideString );
          public
            destructor Destroy; override;
            property Caption: string read FCaption;
          end;
        type
          TItemEnumerator = class( TObject )
          strict private
            FOwner: TXmlFileList;
            FIndex: Integer;
          protected
            constructor Create(AOwner: TXmlFileList);
          public
            function GetCurrent: TItem;
            function MoveNext: Boolean;
            property Current: TItem read GetCurrent;
          end;
      strict private
        FXmlFiles: array of TXmlFileList.TItem;
      strict protected
        function GetXmlFile( index: Integer ): TXmlFileList.TItem;
      public
        type TXmlFile = TXmlFileList.TItem;
        constructor Create( );
        destructor Destroy; override;
        function Add( const ACaption: string; const AXML: WideString ): Integer; overload;
        function Add( const AFilename: string ): Integer; overload;
        function Count: Integer;
        function GetEnumerator: TItemEnumerator;
        procedure Clear;
        property XmlFile[ index: Integer ]: TXmlFileList.TItem read GetXmlFile; default;
      end;
    

    And yes, you can start scratching your head when you look at it, but it’s a great solution to hide many functions from inexperienced developers! Now they only see what they need to see. (And hopefully they never look at this sourcecode!)
    I just expected that I needed to write more code than this…


    Why exposing the TItem type with a different name? Actually, these classes are wrapped again in a bigger class TXmlManager that also handles stylesheets, transformations, validations and a bunch of other stuff. TXmlFile is actually exposed at the TXmlManager level, not the TXmlFileList class. TXmlManager contains a few other similar lists where I just re-use the name TItem. There are no conflicts, though, because the parents need to be added to refer to the proper class type.
    And while the class header might look complex with nearly 200 lines of code, the rest of the unit is quite slim, with almost 700 lines of code and plenty of comments. The class structure is actually helping me to make it all look simply from the viewpoint of those who use it. Those who use it, don’t need to look for types and methods to use. Their choices are just very limited…

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

Sidebar

Related Questions

I knew this once but I keep forgetting; How do I allow multiple filetypes
Edit: Somehow I knew, after two days messing with this, that I'd figure it
i am looking for a specific data structure, but i forgot its name. if
I could have sworn I knew how to do this ... regardless, I want
i was wondering if anyone knew any widgets for creating a tag cloud that
Okay this is a real headscratcher. I have an application which calls a web
I'm looking for a text editor that can show me the actual carriage returns
How do I allocate memory for a char variable (not a char pointer) inside
I am building a game in Visual Studio 2008 and in order to build
We recently went into private beta on our flagship product and had a small

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.