Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - cdubet

Pages: [1]
1
Why I my new element is not in list ??

Element^ CodeViewPackage::AddElementType(Element^ elem, String^ type, String^ name) {
int nb=elem->EmbeddedElements->Count; // old value


//create a new item
Element^% el = safe_cast<Element^>(elem->EmbeddedElements->AddNew(name,”test”));
       if (el ==nullptr)    {
         log->Error("AddElementType failed: element=nullptr!");
         return nullptr;
       }
el->Update();
elem->EmbeddedElements->Refresh();
int nb2=elem->EmbeddedElements->Count;
// nb2== nb !!!
         

2
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

Pages: [1]