Book a Demo

Author Topic: Help with collections  (Read 3612 times)

DanaMaxwell

  • EA User
  • **
  • Posts: 24
  • Karma: +0/-0
    • View Profile
Help with collections
« on: June 12, 2018, 01:48:34 am »
Hi All,

I have been struggling with using collections. I want to setup a temporary collection. The goal is to traverse the model via links but to make sure I do not put duplicates in my collection.

I am a VB newbie so I am sure this is something simple that I am not grasping but here goes.

I am trying to add an item to my collection but get the error "DanaTest.dummyCollection error: Object required: 'dummyColl', Line:29"

I have tried several variants to the code below with no luck.

code is below

option explicit

!INC Local Scripts.EAConstants-VBScript

'
' using a collection

'
' Related APIs
' =================================================================================
' Package API - http://www.sparxsystems.com/enterprise_architect_user_guide/12.1/automation_and_scripting/package_2.html
' Repository API - http://www.sparxsystems.com/enterprise_architect_user_guide/12.1/automation_and_scripting/repository3.html
'

sub dummyCollection()

   ' Show the script output window
   Repository.EnsureOutputVisible "Script"
   
   Session.Output( "VBScript Dummy collection " )
   Session.Output( "=======================================" )
   
   dim dummyColl as EA.Collection
   dim dummyEle as EA.Element

   dim contextObjectType
   contextObjectType = Repository.GetContextItemType()

   set dummyEle = dummyColl.AddNew("dummy1","Class")
   dummyEle = GetContextObject()

end sub

dummyCollection

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Help with collections
« Reply #1 on: June 12, 2018, 04:53:32 pm »
Hi Dana,

EA.Collection is not a "regular" collection type.
It can be used to iterate elements in EA (e.g. Element.Attributes) or to create new objects in EA (e.g. set myNewAttrbute = Element.Attributes.AddNew("attributeName", "string))

You cannot use it as a general purpose collection type.
Instead you can either use an array (which is often annoying since it has a fixed size) or use a .Net ArrayList.
To create a new ArrayList use:

Code: [Select]
dim myList
set myList = CreateObject("System.Collections.ArrayList")

Geert