Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: cdubet on August 29, 2006, 10:08:11 am

Title: AddNew+Update Shall we get a new address
Post by: cdubet on August 29, 2006, 10:08:11 am
As I was experimenting some exception in my C++ code, I made some tests and discovered that I should not use the object created with AddNew after the call to Update() and Refresh()

Questions
1) Am I right ?
2) Why? Is there a global rule to know ? (when should I reload the object, when is it OK to use the AddNew result, Can I always assume that the last item is the latest inserted) ?

For example

Method^ SimulatePlugin::AddElementMethod(Element^ elem, String^ returnType, String^ methodName) {
 Method^% meth = safe_cast<Method^>(elem->Methods->AddNew(methodName, returnType));

   if (meth ==nullptr)    {
     log->Error("AddElementMethod failed: meth=nullptr!");
     return nullptr;
   }
   try {
   meth->Update();
   elem->Methods->Refresh();
   }
   catch (Exception^ ex) {
     String^ err = meth->GetLastError();
     log->Error("AddElementMethod failed:"+err,ex);
     return nullptr;
   }

   Method^% meth2 = safe_cast<Method^>(elem->Methods->GetAt(elem->Methods->Count-1)); // assume it has been inserted in the last position
return meth2;
}

Returning meth instead of meth2 will lead to a Exception in :

Parameter^ SimulatePlugin::GetMethodParameter(Method^ meth) {
 for(int i = 0; i < meth->Parameters->Count; i++) {
     Parameter^% param = safe_cast<Parameter^>(meth->Parameters->GetAt(i)); // exception here