Book a Demo

Author Topic: How to check if a Element exist on the diagram  (Read 5128 times)

Mithal

  • EA User
  • **
  • Posts: 27
  • Karma: +0/-0
    • View Profile
How to check if a Element exist on the diagram
« on: February 08, 2011, 08:01:57 pm »
Hi.

How can i check if an Element with Name "CustomerName" already exist on the diagram

I tried newDiagram.DiagramObjects.GetByName("CustomerName");

and it throws an exception.

Thanks in advance.

SomersetGraham

  • EA User
  • **
  • Posts: 376
  • Karma: +1/-0
    • View Profile
Re: How to check if a Element exist on the diagram
« Reply #1 on: February 08, 2011, 08:14:52 pm »
Mithal

This is the expected behaviour if the collection does not contain "CustomerName"

see http://www.sparxsystems.com/enterprise_architect_user_guide/8.0/automation_and_scripts/collection.html

HTH

Graham
Using V12

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: How to check if a Element exist on the diagram
« Reply #2 on: February 08, 2011, 08:33:57 pm »
Mithal,

I don't ever use Collection.GetByName as it doesn't work on all collections, and the results are random when there are multiple elements with the same name.
You better do a foreach and check the elements yourself. (or use an sqlQuery if performance is an issue)


Geert

Mithal

  • EA User
  • **
  • Posts: 27
  • Karma: +0/-0
    • View Profile
Re: How to check if a Element exist on the diagram
« Reply #3 on: February 08, 2011, 09:10:55 pm »
Hi, i am using this code

EA.Element textElement = (EA.Element)parentPackage.Elements.GetByName("CustomerName");

Seems to work.

Please advise if ishould not be doing this.  I appreciate you guys response to my query.
Thanks

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: How to check if a Element exist on the diagram
« Reply #4 on: February 08, 2011, 09:19:17 pm »
Mithal,

What happens if you have two (or more) elements with the name "CustomerName" in your package?
Which one will be returned? -> result is unpredictable.

Geert

Mithal

  • EA User
  • **
  • Posts: 27
  • Karma: +0/-0
    • View Profile
Re: How to check if a Element exist on the diagram
« Reply #5 on: February 08, 2011, 09:22:46 pm »
Thats good point.  What i will do is give the text Element a Name like zzzSYSPROCompanyName, something not very auvious.

but i will use a loop so i update the first occurence.

Thanks for your Help.