Jump to content

Recommended Posts

  • Moderators

hm, this works (of course)

  var list : array of variant;

  list.add(class fieldA = 'fieldA1'; fieldB = 'fieldB1'; end);
  list.add(class fieldA = 'fieldA2'; fieldB = 'fieldB2'; end);
  list.add(class fieldA = 'fieldA3'; fieldB = 'fieldB3'; end);

 

Link to post
Share on other sites

Another way to create literal array of objects:

type
  THandleArray = array of variant;

function list: THandleArray;
begin
result := [
  CLASS
    fieldA  := 'fieldA1';
    fieldB  := 'fieldB1';
  END,
  CLASS
    fieldA  := 'fieldA2';
    fieldB  := 'fieldB2';
  END,
  CLASS
    fieldA  := 'fieldA3';
    fieldB  := 'fieldB3';
  END];

end;


  console.log(
    JSON.stringify(Variant(list))
  );

output:
[{"fieldB":"fieldB1","fieldA":"fieldA1"},{"fieldB":"fieldB2","fieldA":"fieldA2"},{"fieldB":"fieldB3","fieldA":"fieldA3"}]

Cheers,
warleyalex

Link to post
Share on other sites
  • Moderators

I wanted to create the list of objects above using anonymous classes and use it as an inline parameter to a function

something like

  var MyFunc := MyObject.MyFunction(class
      fieldZ = class
        a = 'b';
      end;
    end);

which defaults to

var MyFunc := MyObject.MyFunction(fieldZ);


and which works great in almost all cases.

However I couldn't get the array with the list of objects to behave in this way ...

Anyway, the solutions in this thread are good enough. Thanks

Link to post
Share on other sites
  • 2 months later...
  • Moderators

This statement doesn't compile : Syntax Error: Name "length" is reserved [line: 101, column: 80, file: Form1]

class width=10; length=12

due to the 'length' identifier. 

'&length' doesn't work either

Any workaround for this ?

edited :

json.parse('{"width":10,"length":12}')

works around this, just not using anonymous classes

Link to post
Share on other sites
function list: THandleArray;
begin
result := [
  CLASS
    l  := 'fieldA1'; external 'length';
    w  := 'fieldB1'; external 'width';
  END,
  CLASS
    l  := 'fieldA2'; external 'length';
    w  := 'fieldB2'; external 'width';
  END,
  CLASS
    l  := 'fieldA3'; external 'length';
    w  := 'fieldB3'; external 'width';
  END];

end;

[{"width":"fieldB1","length":"fieldA1"},{"width":"fieldB2","length":"fieldA2"},{"width":"fieldB3","length":"fieldA3"}]

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...