Book a Demo

Author Topic: Element.Equals  (Read 3716 times)

canderson

  • EA Novice
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Element.Equals
« on: March 08, 2008, 12:45:28 am »
Hi,

Has anyone had any experience of using element.equals?

I'm trying to compile a distinct list of use case (elements) displayed on n Use Case Diagrams.

I was planning to use a List<element> to do this and take advantage of the list.contains method to see whether or not to add the element to the collection.

Anyway, my first cut didn't work so now I've done some testing and  I've drastically simplified my code to that below. I'm getting an unexpected result - ie. x.Equals(y) returns false;

Code: [Select]

EA.ElementClass x = (EA.ElementClass)eaModel.GetElementByGuid("{C49C6146-4DDA-4133-B86E-D9077C92142A}");

EA.ElementClass y = (EA.ElementClass)eaModel.GetElementByGuid("{C49C6146-4DDA-4133-B86E-D9077C92142A}");

MessageBox.Show(x.Equals(y).ToString());


I'm thinking now I need to create a new class that inherits from ElementClass and override the Equals method to compare the elementguids - or have I overlooked something?

Thanks.

CA.

Frank Horn

  • EA User
  • **
  • Posts: 535
  • Karma: +1/-0
    • View Profile
Re: Element.Equals
« Reply #1 on: March 08, 2008, 02:58:01 am »
Does this also happen if you cast to EA.Element instead of EA.ElementClass? I think ElementClass is some witchcraft thing that Visual Studio creates within the COM interop layer when you add a reference to the EA object library.

So maybe you have two instances of EA.ElementClass wrapping the same EA.Element instance.

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Element.Equals
« Reply #2 on: March 08, 2008, 03:51:24 am »
You are correct Frank.

Chris,

I've been successful in getting around this through the back door. I have usually just created something like a Dictionary(Of String, EA.Element). Now I can add the elements using their ElementGUID property as the key. The ContainsKey method let's me query the collection.

David
No, you can't have it!

canderson

  • EA Novice
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Re: Element.Equals
« Reply #3 on: March 10, 2008, 08:33:51 pm »
Thanks David,

I tried several things in vain before resorting to your suggestion, which although it isn't optimal code it is 100% reliable!

The nice thing about the dictionary is it exposes a .values property so it's still pretty simple to iterate through each item in the collection.

Thanks again.

Chris.
« Last Edit: March 10, 2008, 08:34:31 pm by canderson »

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Element.Equals
« Reply #4 on: March 10, 2008, 09:45:51 pm »
You can also use foreach, just remember that you will get a key-value pair. Also remember that the order of retrieval is based on the keys, so it might not be what you expect.

There are ContainsKey and ContainsValue [check that in case I'm not quite correct - I am using VS 2008 docs] methods.

Remember that you could also use SortedDictionary and SortedList with generic key and value parameters.

Finally, if you do a lot of value lookups you might want to write a wrapper class around EA.Element. This could expose a comparison operator that compared the GUID (for example) and returned an answer. This could result in better performance if you do this a lot.

HTH, David
No, you can't have it!