Book a Demo

Author Topic: Delete a connector  (Read 3766 times)

Maggie

  • EA User
  • **
  • Posts: 106
  • Karma: +0/-0
    • View Profile
Delete a connector
« on: February 08, 2014, 03:51:08 am »
Hi
I am trying to delete a connector from within my addin -
Code: [Select]
                   short index = 1;
                    foreach (EA.Connector conn in clientElement.Connectors)
                    {
                        //EA.Element clientElement = Repository.GetElementByID(connector.ClientID);
                        if (conn.Type == "Generalization" &&
                            clientElement.Stereotype == "abc" &&
                            conn.SupplierID == QObjectElement.ElementID)
                        {
                            clientElement.Connectors.Delete(index);
                            conn.Update();
                            clientElement.Refresh();
                            clientElement.Update();
                            supplierElement.Refresh();
                            supplierElement.Update();
                            break;
                        }
                        index++;

When I debug the addin the code to delete the connector is executed but the connector is not deleted from the model

I would be grateful if someone could tell me what I am doing wrong

Thanks

Maggie

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Delete a connector
« Reply #1 on: February 08, 2014, 09:04:13 am »
I'm not sure whether the collector iterator has the same order as your index. So I'd prefer to use GetAt to be sure that index and element to be deleted match.


Further you don't need the refresh and update methods. You should only call Connectors.Refresh AFTER the whole loop.

q.

P.S. I just recognized that I did not really explain Delete in my Scripting book. I shall add that...
« Last Edit: February 08, 2014, 09:07:55 am by qwerty »

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 a connector
« Reply #2 on: February 10, 2014, 06:05:30 pm »
Maggie,

You are doing some weird stuff here.
* You are using a foreach loop, but then still want to use a counter... Why don't you simply use a for loop?
Code: [Select]
for (int i = 0; i< clientElement.Connectors.Count; i++)* When you are deleting from a collection you are looping then you should always start at the end. So the above loop should become
Code: [Select]
for (int i = clientElement.Connectors.Count -1; i >=0;i--) * right after you delete the connector from the element you save it again by calling conn.Update(). You should remove that call
* you don't need any of the update() and refresh() calls as qwerty already indicated.

Geert
« Last Edit: February 10, 2014, 06:05:55 pm by Geert.Bellekens »