#1 2023-05-11 20:56:10

larand54
Member
Registered: 2018-12-25
Posts: 104

Problem adding object to an ObjArray in a service.

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

#2 2023-05-11 21:35:11

tbo
Member
Registered: 2015-04-20
Posts: 353

Re: Problem adding object to an ObjArray in a service.

larand54 wrote:

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

#3 2023-05-12 06:48:49

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,660
Website

Re: Problem adding object to an ObjArray in a service.

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

#4 2023-05-12 09:08:12

larand54
Member
Registered: 2018-12-25
Posts: 104

Re: Problem adding object to an ObjArray in a service.

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. smile


Delphi-11, WIN10

Offline

Board footer

Powered by FluxBB