Book a Demo

Author Topic: Delete all package elements  (Read 3215 times)

GrahamL

  • EA User
  • **
  • Posts: 111
  • Karma: +2/-0
    • View Profile
Delete all package elements
« on: June 25, 2020, 01:30:00 am »
Hi
I want to delete all elements in a package from an add
This is what I am doing
               var elements = contextPackage.Elements;
               
                for (short i = 0; i < elements.Count; ++i)
                {
                    elements.Delete(i);
                    elements.Refresh();
                }
                contextPackage.Update();
But i seems not to work
What is the correct way?

Thanks

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Delete all package elements
« Reply #1 on: June 25, 2020, 01:41:40 am »
Delete from top to bottom and in all cases remove the Refresh. Or (to make it slower) delete the first and refresh until it's empty.

q.

Uffe

  • EA Practitioner
  • ***
  • Posts: 1859
  • Karma: +133/-14
  • Flutes: 1; Clarinets: 1; Saxes: 5 and counting
    • View Profile
Re: Delete all package elements
« Reply #2 on: June 25, 2020, 01:45:29 am »
Hi,

Your exit condition depends on the size of a collection you're whittling down. This gets tricky.
The simple solution is to loop backwards, and only refresh after the end of the loop.

/Uffe
My theories are always correct, just apply them to the right reality.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Delete all package elements
« Reply #3 on: June 25, 2020, 01:59:08 am »
I do as Uffe says, loop from back to front.
Not really necessary if you don't refresh, but a "best practice" anyway

Example vbscript
Code: [Select]
dim j
for j = package.Elements.Count -1 to 0 step -1
package.Elements.DeleteAt j , false
next

Example C#
Code: [Select]
            for (int i = this.elementWrapperOwner.wrappedElement.Constraints.Count - 1; i >= 0; i--)
            {
                var currentConstraint = (global::EA.Constraint)this.elementWrapperOwner.wrappedElement.Constraints.GetAt(short.Parse(i.ToString()));
                if (this.name == currentConstraint.Name)
                {
                    this.elementWrapperOwner.wrappedElement.Constraints.DeleteAt(short.Parse(i.ToString()), false);
                }
            }

Geert

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Delete all package elements
« Reply #4 on: June 25, 2020, 02:05:18 am »
The refresh at the end of the Delete-All-Loop is quite, hmmm. There's nothing left to refresh.

q.