You are not logged in.
I have two classes TArticle and TSubProject. A SubProject can contain several articles.
Articles can be added by demand to the subproject.
I'm using "ObjArrayAdd" to add new articles to the subproject.
Like: ObjArrayAdd(aSubP.Articles, aArticle); 
But that's not allowed - I get this message from delphi: "Constant object can not be passed as var parameter"
So I tried to use an intermediate variable and that calms delphi but executing the program gives me another error:
"Invalid pointer operation" when the function exits. But the database is updated then.
I' understand that I do something really wrong but how should I do?
The repository:
 
function TDHSSyncRepository.addArticleToSubProject(var aSubP: TSubProject;
  out aArticle: TArticle): TSyncRepoError;
var
  s: TArticleObjArray;
begin
  if RetrieveArticle(aArticle) <> srSuccess then begin
    result := srNotFound;
    exit;
  end;
  Result := srNotFound;
 // ---                         --> ObjArrayAdd(aSubP.Articles, aArticle); <<--- "Constant object can not be passed as var parameter
  s := aSubP.Articles;         //  <<---- Do this instead
  ObjArrayAdd(s, aArticle);  // << -- Works but gives "Invalid operation"
  aSubP.Articles := s;         //
 end;Best regards,
LarsG
Delphi-11, WIN10
Offline
I' understand that I do something really wrong but how should I do?
For me, the following looks clearer:
procedure TSubProject.AddArticle(pmArticle: TArticle);
begin
  if pmArticle <> Nil then
    ObjArrayAdd(FArticles, pmArticle);
end;
function TDHSSyncRepository.AddArticleToSubProject(pmSubProject: TSubProject; out pmoArticle: TArticle): TSyncRepoError;
begin
  Result := srNotFound;
  if pmSubProject = Nil then Exit; //=>
  if RetrieveArticle(pmoArticle) <> srSuccess then Exit; //=>
  pmSubProject.AddArticle(pmoArticle);
  Result := srFound;
end;With best regards
Thomas
Last edited by tbo (2023-05-11 21:43:29)
Offline
As I wrote above, the class instances are allocated and freed by the server side of the framework.
So if you want to keep the value of such a class, e.g. inside a list or array, you should first make a copy.
You can use the CopyObject() function for this.
Offline
Thanks Thomas, excelent solution even though it didn't help eliminate the invalid pointer operation.
Thanks ab, your comment woke me up, I asked myself why I use the whole objects in the parameter list when I only need the ID:s.
So I replaced those parameters with the id:s of the objects and now everything is alright again. ![]()
Delphi-11, WIN10
Offline