Book a Demo

Author Topic: Iterate through tagged names and values  (Read 4851 times)

motivatedgorilla

  • EA User
  • **
  • Posts: 44
  • Karma: +0/-0
    • View Profile
Iterate through tagged names and values
« on: March 07, 2014, 10:58:15 am »
I am attempting to iterate through a set of tagged values for a given element however unable to execute the code as it displays the error an object is required. I'm probably doing something wrong however am unsure what

Code: [Select]
var theElement as EA.Element;
var tag as EA.TaggedValue;
theElement = Repository.GetElementByID(52)
for ( var i = 0 ; i < theElement.TaggedValues.Count ; i++ )
 {
            thisTaggedValue = theElement.TaggedValues.GetAt(i);
            If (thisTaggedValue.ElementID == tag.ElementID)
            {
                  Session.Output(tag.Value);
            }
            
 }

KP

  • EA Administrator
  • EA Expert
  • *****
  • Posts: 2919
  • Karma: +55/-3
    • View Profile
Re: Iterate through tagged names and values
« Reply #1 on: March 07, 2014, 11:24:46 am »
tag is declared and used but not given a value, thisTaggedValue is given a value but not declared, and you're missing a ; on line 3.
« Last Edit: March 07, 2014, 11:25:17 am by KP »
The Sparx Team
[email protected]

motivatedgorilla

  • EA User
  • **
  • Posts: 44
  • Karma: +0/-0
    • View Profile
Re: Iterate through tagged names and values
« Reply #2 on: March 07, 2014, 11:29:45 am »
Quote
tag is declared and used but not given a value, thisTaggedValue is given a value but not declared, and you're missing a ; on line 3.

Can you give me an example of how i can assign a value to tag and what thisTaggedValue should be declared as?

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Iterate through tagged names and values
« Reply #3 on: March 07, 2014, 11:38:30 am »
AFAICS you should declare
Code: [Select]
var thisTag as EA.TaggedValue;
q.

Nizam Mohamed

  • EA User
  • **
  • Posts: 193
  • Karma: +1/-0
    • View Profile
Re: Iterate through tagged names and values
« Reply #4 on: March 07, 2014, 02:25:58 pm »
Please refer to VBScript / JScript Element Extras Example (Tools->Scripting) in EA to see how tagged values are iterated through.
Code excerpt below
Code: [Select]
           // Add a new tag
            var tags as EA.Collection;
            tags = theElement.TaggedValues;
            
            var newTag as EA.TaggedValue;
            newTag = tags.AddNew( "MyTag", "Number" );
            newTag.Update();
            tags.Refresh();
            
            Session.Output( "    Added new Tag: " + newTag.Name );
            
            newTag = null;
            
            // List all element tag
            for ( var i = 0 ; i < tags.Count ; i++ )
            {
                  var currentTag as EA.TaggedValue;
                  currentTag = tags.GetAt( i );
                  
                  Session.Output( "    Tag Value: " + currentTag.Name );
                  
                  // Delete the tagged value that we just added
                  if ( currentTag.Name == "MyTag" )
                  {
                        tags.DeleteAt( i, false );
                        Session.Output( "    Deleted Tag: " + currentTag.Name );
                  }
            }