Book a Demo

Author Topic: Adding a Element to a diagram  (Read 4154 times)

Rossco

  • EA Novice
  • *
  • Posts: 10
  • Karma: +0/-0
    • View Profile
Adding a Element to a diagram
« on: February 02, 2011, 12:07:44 pm »
Hi I am trying to add an element to a diagram using C#
I have the diagram and the element and a using the sample VB.net based code

EA.Diagram MyDiagram = MyRepository.GetCurrentDiagram();
                    EA.DiagramObject O;
                   O = MyDiagram.DiagramObjects.AddNew("l=200;r=400;t=200;b=600;", "");

I get the following error

Cannot implicitly convert type 'object' to 'EA.DiagramObject'. An explicit conversion exists (are you missing a cast?)

Any Ideas on how this should work

Regards

Ross      

Aaron B

  • EA Administrator
  • EA User
  • *****
  • Posts: 941
  • Karma: +18/-0
    • View Profile
Re: Adding a Element to a diagram
« Reply #1 on: February 02, 2011, 04:03:52 pm »
Like the message from Visual Studio suggests - you need to explicitly cast the object returned from AddNew to an EA.DiagramObject type.  E.g.

EA.Diagram MyDiagram = repository.GetCurrentDiagram();
EA.DiagramObject O;
O = (EA.DiagramObject)MyDiagram.DiagramObjects.AddNew("l=200;r=400;t=200;b=600;", "");

Same principle will also apply when using other collections such as Elements, Packages, etc.

Paolo F Cantoni

  • EA Guru
  • *****
  • Posts: 8626
  • Karma: +259/-129
  • Inconsistently correct systems DON'T EXIST!
    • View Profile
Re: Adding a Element to a diagram
« Reply #2 on: February 02, 2011, 04:05:06 pm »
Quote
...
                   O = MyDiagram.DiagramObjects.AddNew("l=200;r=400;t=200;b=600;", "");
Hi Ross,

You need to explicitly cast the (generic) Object returned from AddNew... Thus:

O = (EA.DiagramObject)MyDiagram.DiagramObjects.AddNew("l=200;r=400;t=200;b=600;", "");

Bit of a PITA, but once you get used to it...

HTH,
Paolo
« Last Edit: February 02, 2011, 04:05:48 pm by PaoloFCantoni »
Inconsistently correct systems DON'T EXIST!
... Therefore, aim for consistency; in the expectation of achieving correctness....
-Semantica-
Helsinki Principle Rules!

Rossco

  • EA Novice
  • *
  • Posts: 10
  • Karma: +0/-0
    • View Profile
Re: Adding a Element to a diagram
« Reply #3 on: February 03, 2011, 05:41:48 am »
Thanks guys ;D

Oh - how easy in hindsight!