Author Topic: Create Tagged Value doesn't work  (Read 3317 times)

jörg

  • EA User
  • **
  • Posts: 28
  • Karma: +0/-0
    • View Profile
Create Tagged Value doesn't work
« on: March 21, 2020, 04:02:19 am »
Hi all,

after many hours of trying and searching I don't see another way than to ask the specialists here. It is not my first EA-Addin I develop but I fail in creating a Tagged Value for a Package. This is my code:

Code: [Select]
            string error;
            bool updated;

            EA.Package internalPackage = repo.GetPackageByGuid(packageGUID);

            EA.TaggedValue taggedValueConfig = internalPackage.Element.TaggedValues.AddNew("tvname", "Memo");

            taggedValueConfig.Notes = "some notes text";
            taggedValueConfig.Value = "some value text";

            updated = internalPackage.Element.Update();
            if (!updated) error = internalPackage.Element.GetLastError();

            internalPackage.Element.TaggedValues.Refresh();

            updated = internalPackage.Update();
            if (!updated) error = internalPackage.Element.GetLastError();

AddNew gives me a valid tagged value object. Nowhere an error is shown. 'updated' is always true. But at the end the tagged value is not added to the collection.

On one hand the API documentation writes, that the 'TaggedValue' collection of the 'Element' class is readonly, which would explain the failed adding. Also the documentation for the 'Element' attribute of the package class says:
Quote
Element

Notes: Read only

The associated element object; use to get/set common information such as Stereotype, Complexity, Alias, Author, Constraints, Tagged Values and Scenarios.
So it states it is readonly but then it says it is used to get/set - amonst others - tagged values.

On the other hand I find many examples in the web for adding tagged values to elements (even in Ea-Documents, e.g. see https://sparxsystems.com/resources/user-guides/14.0/automation/automation.pdf, page 298), 'AddNew' returns a valid tagged value object and throws no exception and I can't imagine any reason why the 'TaggedValue' collection of the 'Element' class should be readonly.

I think there must be a way to add new tagged values to a package element. So please help me and tell me, what I'm doing wrong. I'm helpless and maximum confused. I know, it's a lot of text, but I wanted to give as much information as possible.

I use EA14 on a Windows 10 machine and I'm programming with Microsoft Visual Studio Professional 2019.

Thanks in advance,
Jörg
« Last Edit: March 21, 2020, 04:05:39 am by jörg »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13282
  • Karma: +556/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Create Tagged Value doesn't work
« Reply #1 on: March 21, 2020, 06:12:13 pm »
You forgot to update() the new tagged value.

Every time you create an object with a Collection.AddNew() you get an object that only exists in memory.
To persist it to the database you have to call update() on it.


Code: [Select]
            EA.TaggedValue taggedValueConfig = internalPackage.Element.TaggedValues.AddNew("tvname", "Memo");

            taggedValueConfig.Notes = "some notes text";
            taggedValueConfig.Value = "some value text";
            taggedValueConfig.Update(); //this actually saves the new tagged value to the database

Geert

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13282
  • Karma: +556/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Create Tagged Value doesn't work
« Reply #2 on: March 21, 2020, 06:14:04 pm »
The "readonly" property only indicates that you cannot set this property.

You cannot do

Code: [Select]
myPackage.Element = someOhterElement;
It doesn't mean the object stored in this property cannot be chaned.

Geert

jörg

  • EA User
  • **
  • Posts: 28
  • Karma: +0/-0
    • View Profile
Re: Create Tagged Value doesn't work
« Reply #3 on: March 21, 2020, 09:22:44 pm »
Hello Geert,

thanks a lot again for your help!! This solved my problem and I can enjoy my vacation week (as far as possible in these times). I was aware that I have to call 'Update' in several cases but I didn't know that I have to do this for all database objects. You'll never stop learning :), also the fact that the 'readonly' only means the variable where the object is stored in is const and not the object itself is a const object. And I wasn't surprised that again you has been the one who helped me out.
After solving this I struggled a bit with setting a long text as value but then I read again the documentation of the TaggedValue class and found the note about long text in tagged values.  ::) So sometimes RTFM is really helping  :)

Jörg