web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

How to delete item in list while iterating through the list?

(0) ShareShare
ReportReport
Posted on by 30

Hi in X++, is it possible to delete items while iterating through list and not causing error?   I know it's doable in C# but not sure how x++ language handle it.

I have the same question (0)
  • Suggested answer
    ergun sahin Profile Picture
    8,826 Moderator on at

    at the moment, I can't look for 365 but at 2012 for iterating a list you can use listIterator or listEmulator. ListIterator has delete method but emulator dont have such a method. In 2012 I don't use iterator (server/client side pointer problem) but I don't think 365 have this problem

    https://docs.microsoft.com/en-us/dotnet/api/microsoft.dynamics.ax.xpp.listiterator

    www.google.com/.../

  • Suggested answer
    Hossein.K Profile Picture
    6,648 on at

    Hi,

    To remove objects from a list you need to use an iterator.

    In X iterators require the developer to write a loop with calls to more() and next() to check if there are any elements left and make iterator advance to the next element.

    list = new List(Types::String);
        list.addEnd('a'); list.addEnd('b'); list.addEnd('c'); list.addEnd('d');
     
        iter = new ListIterator(list);
     
        while (iter.more())
        {
            value = iter.value();
     
            if (true) // check value
            {
                iter.delete();
            }
            else
            {
                iter.next();
            }
        }
     
        info(list.toString());

    Either do a delete() or a next(). Not both. The same goes for maps and sets.

    And by the way, if you want to do anything other than delete elements it’s highly recommended you use enumerators instead of iterators.

  • Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    You can also consider alternative designs. For example, you can have a Set with all items and add items to be deleted to another Set. Then you'll use Set::difference() to create a set without deleted items:

    Set remainingItems = Set::difference(allItems, deletedItems);

  • huijij Profile Picture
    19,811 on at

    Hi Yvonne,

    As the upstairs insight,Here is a link about the Iterator for deleting item in list:

    Enumerator and Iterator

    regards

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 664 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 522 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 303 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans