Jump to content
Sign in to follow this  
lynkfs

Confused on variants

Recommended Posts

Confused on the following errors while setting up a tiered data structure (f.i. business with multiple departments with multiple staff per department)

procedure TForm1.InitializeForm;
begin
  inherited;
  // this is a good place to initialize components

  var MyList1 : variant := new JObject;
  MyList1.Business := TVariant.CreateObject;
  MyList1.Business.Name := 'MyBusiness';
  MyList1.Business.Depts := TVariant.CreateArray;
  MyList1.Business.Depts.push(class DeptName := 'DepartmentA'; Staff := ['st1','st2']; end);
  MyList1.Business.Depts.push(class DeptName := 'DepartmentB'; end);
  MyList1.Business.Depts.push(class DeptName := 'DepartmentC'; end);

  writeln(JSON.stringify(MyList1));               //OK so far
  //{"Business":{"Name":"MyBusiness","Depts":[{"Staff":["st1","st2"],"DeptName":"DepartmentA"},{"DeptName":"DepartmentB"},{"DeptName":"DepartmentC"}]}}

  MyList1.Business.Depts[0].Staff[2] := 'st3';    //works

  MyList1.Business.Depts[0].Staff.add('st4');     //compiles but gives execution error :
  //Uncaught TypeError: MyList1.Business.Depts[0].Staff.add is not a function

  MyList1.Business.Depts[0].Staff.push('st5');    //works. Why does 'push' work but 'add' doesn't ?

  MyList1.Business.Depts[0].staff.push('st6');    //lowercase 'staff'. Compiles but gives execution error :
  //Uncaught TypeError: Cannot read property 'push' of undefined

 

An alternative approach :
 

  var MyList2 :=
  class
    Entries := [
      class
        BusName := 'Bus01';
        Depts := [
          class DeptName := 'DeptA'; Staff := ['a1','a2']; end,
          class DeptName := 'DeptB'; Staff := ['b1','b2']; end,
          class DeptName := 'DeptC'; Staff := ['c1','c2']; end
        ];
      end
      ,
      class
        BusName := 'Bus02';
        Depts := [
          class DeptName := 'DeptD'; Staff := ['d1','d2']; end
          ];
      end
    ];
  end;
  writeln(JSON.stringify(MyList2));      //OK so far
  //{"Entries":[{"Depts":[{"Staff":["a1","a2"],"DeptName":"DeptA"},{"Staff":["b1","b2"],"DeptName":"DeptB"},{"Staff":["c1","c2"],"DeptName":"DeptC"}],"BusName":"Bus01"},{"Depts":[{"Staff":["d1","d2"],"DeptName":"DeptD"}],"BusName":"Bus02"}]}


  MyList2.Entries.push(class BusName := 'Bus03'; end);
  //Syntax Error: Array method "push" is restricted to dynamic arrays
  //Syntax Error: Array method "add" is restricted to dynamic arrays

  MyList2.Entries[0].Depts[0].DeptName := 'aaa';
  //Syntax Error: There is no accessible member with name "Depts"

 

There are a number of workarounds to avoid these errors, but I just don't get the picture of why the above errors occur at all.

Any clarifications ?

 

Share this post


Link to post
Share on other sites

//compiles but gives execution error :
  Uncaught TypeError: MyList1.Business.Depts[0].Staff.add is not a function

MyList1.Business.Depts[0].Staff.add('st4');     

//works. Why does 'push' work but 'add' doesn't ?

MyList1.Business.Depts[0].Staff.push('st5');   
 

AFAIU you're working here with raw Variant and Staff is JS array object that doesn't have add function defined only push:

//lowercase 'staff'. Compiles but gives execution error :
  Uncaught TypeError: Cannot read property 'push' of undefined
MyList1.Business.Depts[0].staff.push('st6');   

JS functions and properties are case sensitive so there's no staff defined only Staff.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×