Jump to content

Search the Community

Showing results for tags 'variants'.



More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Categories

There are no results to display.

Forums

  • Welcome to Smart Mobile Studio
    • News and Information
    • Pre-Sales Questions
    • Smart In the Media
    • Smart Contests
    • Meta
  • Smart Mobile Studio Discussion
    • General
    • IDE
    • RTL
    • Code
    • Client Server
    • Platform
    • Graphics
    • Deployment
    • Suggestion box
  • Smart Mobile Studio support
    • Support
    • Bug report
  • General Discussion
    • Pascal
    • Delphi
    • Javascript
    • HTML/HTML5
    • CSS
  • Resources
    • Website
    • Download Smart Mobile Studio

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


Website URL


Location


Interests


Company name


Position

Found 2 results

  1. lynkfs

    Confused on variants

    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 ?
  2. lynkfs

    using variants

    I needed a super chart component and stumbled on 'zoomcharts', a nice js library with good looking chart types. This library even includes a typescript file. However parsing it through the ts2pas tool results in a whopping 3000 lines of code. A bit daunting and of course most of the exported functions will never be used by an average user. The minimum unit for accessing this library cuts it all down to some 20 lines, looking like unit Zoom; interface type JZoomCharts = class external Procedure PieChart(V: Variant); external 'PieChart'; Procedure NetChart(V: Variant); external 'NetChart'; Procedure FacetChart(V: Variant); external 'FacetChart'; Procedure GeoChart(V: Variant); external 'GeoChart'; Procedure TimeChart(V: Variant); external 'TimeChart'; Constructor Create; Destructor Destroy; end; var ZoomCharts external 'ZoomCharts' : JZoomCharts; implementation {$R 'zoomcharts.js'} end. . The method of communicating with the main external functions is defined here using a Variant, which makes life easy. It does negate Pascals strong typing though. I needed to get my head around how to use Variants so the following are three different methods which can be used from the calling program Try 1 : build a variant in javascript procedure TForm1.W3Button5Click(Sender: TObject); var V : Variant; begin V := TVariant.CreateObject; asm var Sjs = { container: document.getElementById("OBJ5"), area: { height: 663 }, data: { preloaded: { values: [ [0, 100], [1000, 200], [2000, 300], [3000, 400], [4000, 500] ], unit: 's' }, timestampInSeconds: true } }; @V = Sjs; end; ZoomCharts.TimeChart(V); end; This works ok. To get rid of the asm sections I used JSON which resulted in try2 : (note : JSON requires specifically double quotes) procedure TForm1.W3Button5Click(Sender: TObject); var V, V1, V2, V3 : Variant; begin V := TVariant.CreateObject; V1 := TVariant.CreateObject; V2 := TVariant.CreateObject; V3 := TVariant.CreateObject; //V1 : container asm var e = document.getElementById("OBJ5"); @V1 = e; end; V.container := V1; //V2 : area; V2.height := 663; V.area := V2; //V3 : data V3 := TVariant.CreateObject; V3 := JSON.Parse('{ ' + ' "preloaded": { ' + ' "values": [ ' + ' [0, 100], ' + ' [1000, 200], ' + ' [2000, 300], ' + ' [3000, 400], ' + ' [4000, 500] ' + ' ], ' + ' "unit": "s" ' + ' }, ' + ' "timestampInSeconds": "true" ' + '}'); showmessage(json.stringify(v3)); V.data := V3; ZoomCharts.TimeChart(V); end; This works too. Try3 was to try and re-introduce a semblance of strong typing using records : type TXXX = record published values : array[0..4] of array[0..1] of integer; &unit : string; end; TYYY = record published preloaded : TXXX; timestampInSeconds : boolean; end; procedure TForm1.W3Button5Click(Sender: TObject); var V, V1, V2, V3 : Variant; R1 : TXXX; R2 : TYYY; begin V := TVariant.CreateObject; V1 := TVariant.CreateObject; V2 := TVariant.CreateObject; V3 := TVariant.CreateObject; //V1 : container asm var e = document.getElementById("OBJ5"); @V1 = e; end; V.container := V1; //V2 : area; V2.height := 663; V.area := V2; //V3 : data V3 := TVariant.CreateObject; R1.unit := 's'; R1.values := [[0, 100], [1000, 200], [2000, 300], [3000, 400], [4000, 500]]; R2.preloaded := R1; R2.timestampInSeconds := true; V3.data := R2; // showmessage(json.stringify(v3)); V.data := V3; ZoomCharts.TimeChart(V); end; which is by far the most compact and understandable code. However this doesn't work for anonymous object fields .
×