delphi - sending a record type as parameter using dwscript -
please consider record:
type tstudent = record name:string; age: integer; class:string; end;
i have class tschool has following function:
function addstudent(lstudent:tstudent):boolean;
i want use class (tschool) in dwsunit, , function too, can't figure out how send record type parameter. how far i've reached:
procedure tform1.dwsunitclassestschoolmethodsaddstudenteval(info: tprograminfo; extobject: tobject); begin info.resultasboolean:=(extobject tschool).addstudent(info.vars['lstudent'].value); end;
but not working, keeps on giving me error incompatible types.
i have defined in dwsunit record tschool, didn't work either. appreciated.
i don't have delphi 2010 @ disposal now, have delphi xe(it should work in d2010 also), here's works me, can of course modify fit needs:
program project1; {$apptype console} uses sysutils ,windows ,dwscomp ,dwscompiler ,dwsexprs ,dwscoreexprs ,dwsrttiexposer ,generics.collections ; // required {$rtti explicit methods([vcpublic, vcpublished]) properties([vcpublic, vcpublished])} {m+} type // student definition tstudent = record name: string; age: integer; aclass: string; end; // student list, use generics tstudentlist = class(tlist<tstudent>); // school class tschool = class(tobject) private fstudentlist: tstudentlist; published constructor create; destructor destroy; override; published procedure addstudent(astudent: tstudent); function getstudentcount: integer; function getstudent(index: integer): tstudent; end; { tschool } procedure tschool.addstudent(astudent: tstudent); begin fstudentlist.add(astudent); end; constructor tschool.create; begin fstudentlist := tstudentlist.create; end; function tschool.getstudentcount: integer; begin result := fstudentlist.count; end; function tschool.getstudent(index: integer): tstudent; begin result := fstudentlist[ index ]; end; destructor tschool.destroy; begin fstudentlist.free; inherited; end; procedure testrecords; var lscript: tdelphiwebscript; lunit: tdwsunit; lprog: idwsprogram; lexec: idwsprogramexecution; begin lscript := tdelphiwebscript.create(nil); lunit := tdwsunit.create(nil); try lunit.unitname := 'mysuperduperunit'; lunit.script := lscript; // expose tstudent record script lunit.exposertti(typeinfo(tstudent)); // expose tschool class script lunit.exposertti(typeinfo(tschool)); // compile simple script lprog := lscript.compile( 'var lschool := tschool.create;'#$d#$a + 'var lstudent: tstudent;'#$d#$a + 'var index: integer;'#$d#$a + 'for index := 0 10 begin'#$d#$a + 'lstudent.name := format(''student #%d'', [index]);'#$d#$a + 'lstudent.age := 10 + index;'#$d#$a + 'lstudent.aclass := ''a-4'';'#$d#$a + 'lschool.addstudent( lstudent );'#$d#$a + 'end;'#$d#$a + 'println(format(''there %d students in school.'', [lschool.getstudentcount]));'#$d#$a + 'lstudent := lschool.getstudent( 5 );'#$d#$a + 'println(''6th student info:'');'#$d#$a + 'println(format(''name: %s''#$d#$a''age: %d''#$d#$a''aclass: %s'', [lstudent.name, lstudent.age, lstudent.aclass]));' ); if lprog.msgs.haserrors begin writeln(lprog.msgs.asinfo); exit; end; try lexec := lprog.execute; except on e: exception writeln(e.message + #$d#$a + lexec.msgs.asinfo ); end; writeln(lexec.result.tostring); lscript.free; end; end; begin try writeln('press enter begin'); readln; testrecords;; readln; except on e: exception writeln(e.classname, ': ', e.message); end; end.
Comments
Post a Comment