Book a Demo

Author Topic: Problem with adding a new package  (Read 5842 times)

mariusz

  • EA User
  • **
  • Posts: 20
  • Karma: +0/-0
    • View Profile
Problem with adding a new package
« on: October 07, 2009, 04:39:02 pm »
Hi
I'd like to realize a quite simple scenario:

1. Finding some root package
2. Adding subpackage
3. Finding the last added subpackage by name

I wrote this code:

*********************
EA.Package pNewPackage = (EA.Package)rootPackage.Packages.AddNew("I level package","Package");

pNewPackage.Update();
rootPackage.Update();


  try
  {
EA.Package pLastAddedPackage = (EA.Package)rootPackage.Packages.GetByName("I level package");
 }
 catch (Exception)
 {

  Console.WriteLine("Oops Exception - not found but why?????");
  }

Console.WriteLine("Subpackages collection size:"+rootPackage.Packages.Count);
************************


Output of this code is
************************

Subpackages collection size:0

************************
But it is very suprised for me. Why "0"? Where is the package which I have added?

Could you give me some explanations for this situation?

best regards
mario
« Last Edit: October 07, 2009, 05:08:25 pm by kjezyna9 »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Problem with adding a new package
« Reply #1 on: October 07, 2009, 05:20:02 pm »
Aha, the dreadfull "Refresh" issue.
Actually the problem you have is related to the (somewhat weird) behaviour of the EA.Collection.
If you add or remove an element in a collection that will not be reflected in that collection.
The collection stays the same untill you call "Refresh" on that collection.
So if you add a
Code: [Select]
EA.Package pNewPackage = (EA.Package)rootPackage.Packages.AddNew("I level package","Package");

pNewPackage.Update();
rootPackage.Packages.Refresh()
Should do it. (there is no need to update the rootpackage since none of its attributes changed)

Geert

mariusz

  • EA User
  • **
  • Posts: 20
  • Karma: +0/-0
    • View Profile
Re: Problem with adding a new package
« Reply #2 on: October 07, 2009, 07:30:07 pm »
Thanks Geert!!

It works :)

Hermes

  • EA User
  • **
  • Posts: 41
  • Karma: +0/-0
    • View Profile
Re: Problem with adding a new package
« Reply #3 on: October 28, 2009, 02:27:57 am »
Hi,

i've a similar problem...

I have to write an add-in that handles a set of packages defined in a separate root model.

When I create the first new package all seems to be fine,
BUT as soon as I try to create another package I get an error message box in EA that says me that an Array out-of-bound problem occurred... ieven if i try to restart EA, the problem still occurs.

it seems that the Collection fails to tracks its size...
but if I try to get the collection size, I got "1" since from the first attempt.
Moreover, if I try to manually delete the package, my code works as with a fresh file (ie. i can create only the first package).

but why?

 :-[

what i've forgot to do?

if may halps, this is a code fragment i use:

............................
 if (myModel.myAssessmentsRoot.Packages.GetByName(this.textAssessmentName.Text) != null)
            {
                MessageBox.Show("The Assessment Name must be unique. please insert another name.", "Warning.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }
            EA.IDualPackage newpak = (EA.IDualPackage)(myModel.myAssessmentsRoot.Packages.AddNew(this.textAssessmentName.Text, AppConfig.EAP_PACKAGE_TYPE));
            newpak.Update();
            myModel.myAssessmentsRoot.Packages.Refresh();
            myModel.myAssessmentsRoot.Update();
............................

Could Someone helpme, please? :)

Best regards,
/D

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Problem with adding a new package
« Reply #4 on: October 28, 2009, 04:29:48 am »
Could this have something to do with the fact that the first level of packages are supposed to be "views" in EA?
(I'm guessing that you are creating packages directly under a root model element).
What happens if you let it run on a regular package an not on a root model?

Geert

Hermes

  • EA User
  • **
  • Posts: 41
  • Karma: +0/-0
    • View Profile
Re: Problem with adding a new package
« Reply #5 on: October 28, 2009, 05:35:34 am »
Thanks Geert for your quick answer,
however despite my poor variable naming,
"myAssessmentsRoot" is yet a first level package:
I've manually created another root model (it is named "myModel").
so I'm working in the second level packages.

Actually, i was thinking that the problem could be my copy_by_value approach, used to wrap EA.Elements objects.

could it be a problem?

I.E. can I save in my Add-in memory space (RAM) EA object safely, or I should save only EA references (GUID)?  ::)





Hermes

  • EA User
  • **
  • Posts: 41
  • Karma: +0/-0
    • View Profile
Re: Problem with adding a new package
« Reply #6 on: October 28, 2009, 07:06:43 am »
finally i've found where the problem is:

Code: [Select]
if (myModel.myAssessmentsRoot.Packages.GetByName(this.textAssessmentName.Text) != null)
           {
               MessageBox.Show("The Assessment Name must be unique. please insert another name.", "Warning.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
               return false;
           }

this check seems to cause a failure when there is at least a packege in the collection inspected.

could someone explain me why this happens?
 :-[

thank you all :)

/Dan

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Problem with adding a new package
« Reply #7 on: October 28, 2009, 03:02:53 pm »
Yeah, of course.
Sorry I didn't spot that earlier.
The EA.Collection sort of works like an array.
If you do a getByName it will throw an error when there is no element with that name in the collection. It's like you are asking for an element with an index that doesn't exist.
You can either catch the exception, or loop all packages and compare their names with "this.textAssessmentName.Text"

Geert

Hermes

  • EA User
  • **
  • Posts: 41
  • Karma: +0/-0
    • View Profile
Re: Problem with adding a new package
« Reply #8 on: October 28, 2009, 09:00:53 pm »
Ok, it works now!

Thank you, Geert  :)