Recent

Author Topic: Modifying TJSArray object members  (Read 282 times)

rlsdevine

  • Newbie
  • Posts: 4
Modifying TJSArray object members
« on: February 08, 2023, 01:58:42 am »
I'm actually using pas2js from TMS WebCore (Delphi) but this question seems to be a pas2js thing. I have a TJSArray (retrieved via JSON from an API call) that holds a number of objects that define users, e.g. in Object Pascal:

Code: Pascal  [Select][+][-]
  1.   TUser = record
  2.     FirstName: string;
  3.     Surname: string;
  4.     DisplayName: string;
  5.   end;

My TJSArray is a private field variable of the class:

Code: Pascal  [Select][+][-]
  1.   private
  2.     FUserList: TJSArray;

I'm testing different ways of modifying members of the array:

Code: Pascal  [Select][+][-]
  1.   // -- The following works correctly.
  2.   asm
  3.     this.FUserList[0].DisplayName = "Bob1";
  4.   end;
  5.   console.log('DisplayName: ' + TUser(FUserList[0]).DisplayName);
  6.  
  7.   // -- The following line fails to compile: "Variable identifier expected".
  8.   //TUser(FUserList[0]).DisplayName := 'Bob2';
  9.   //console.log('DisplayName: ' + TUser(FUserList[0]).DisplayName);
  10.  
  11.   // -- The following works correctly.
  12.   TJSObject(FUserList[0]).Properties['DisplayName'] := 'Bob3';
  13.   console.log('DisplayName: ' + TUser(FUserList[0]).DisplayName);

The puzzle is that I can hard cast TUser(FUserList[0]) to get the DisplayName for the console output, but can't get it to compile when setting the value. Obviously not a show-stopper but curious how to get around this. I also tried (FUserList[0] as TUser) but that won't compile either.

Thanks, Bob


 

TinyPortal © 2005-2018