Author Topic: Help needed creating tagged values in EA with C#  (Read 6459 times)

IanG

  • EA Novice
  • *
  • Posts: 9
  • Karma: +0/-0
    • View Profile
Help needed creating tagged values in EA with C#
« on: November 06, 2015, 10:59:24 pm »
Hello all,

I'm trying to build an add-in which allows the user to create a tagged value through a windows form. The user selects the package and then the element which will have the next tagged value.

The code compiles fine, EA and the add-in run fine but the new tagged value simply does get added. Can anyone see anything wrong in my approach? I've played around with the refresh and the update but no luck so

Thanks in advance.

Ian




            string SelectedElement = treeEA.SelectedNode.Text;      

            EA.Package aPackage;
            aPackage = EA_Repository.GetTreeSelectedPackage();


            foreach (EA.Package thePackage in aPackage.Packages)
            {

                foreach (EA.Element theElements in thePackage.Elements)
                {

                    if (SelectedElement == theElements.Name)
                    {      
                        theElements.TaggedValues.AddNew("MyTagName", "MyTagValue");
                        theElements.TaggedValues.Refresh();
                        theElements.Update();

                          
                    
                    }
                }
          
                 }

        

« Last Edit: November 06, 2015, 10:59:57 pm by IanG »

Uffe

  • EA Practitioner
  • ***
  • Posts: 1859
  • Karma: +133/-14
  • Flutes: 1; Clarinets: 1; Saxes: 5 and counting
    • View Profile
Re: Help needed creating tagged values in EA with
« Reply #1 on: November 06, 2015, 11:42:05 pm »
Hi Ian,


Collection.AddNew() returns the object that's been created, in this case a TaggedValue, and you need to call Update() on that or it will never get written to the database. You're refreshing the collection, good, and updating the element (not sure if you actually need to do that in this case), but you never update the newly created tagged value.

There's a sample in the help under Automation and Scripting -- Enterprise Architect Object Model -- Reference -- Code Samples -- Element Extras.

Cheers,


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

jan.izyk

  • EA Novice
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: Help needed creating tagged values in EA with
« Reply #2 on: November 07, 2015, 12:10:38 am »
Hi Ian,

your code has just two small mistakes.
The first has been answered by Uffe.
The AddNew-function returns an object which will be your new tagged-value.

The second is, that you have not to call the Update-function of collection. You have to call the Update-function of your new tagged value.

Also the parameter of the AddNew-function are wrong. The first parameter is the name of the tagged-value (this is correct) but he second parameter is the type of your tagged-value not the value.

When I change your code, it should be corrected look like this
Code: [Select]
EA.TaggedValue newValue=(EA.TaggedValue)theElements.TaggedValues.AddNew("MyTagName", "string");
newValue.Value="MyTagValue";
newValue.Update()
theElements.TaggedValues.Refresh();

Jan

IanG

  • EA Novice
  • *
  • Posts: 9
  • Karma: +0/-0
    • View Profile
Re: Help needed creating tagged values in EA with
« Reply #3 on: November 07, 2015, 12:44:24 am »
Excellent - that works great! :)
Thank you both so much for your help.

Cheers,
Ian