Forum > General
Variant Records with Real and String (Ansi) types
(1/1)
Nafees:
I'm making a scripting engine, weak typed. For that I need variant records, I've read how they work, but they won't work with Real or Strings. This is how I declare it:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type TMp = packed record case Real of //Error: Ordinal expression expected 0: (R: Real); 1: (S: String);//Error: Data types which require initialization/finalization cannot be used in variant records end;
How do I get it to work?
Thaddy:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type TMp = packed record case integer of 0: (R: Real); 1: (S: String[255]); end;
That's because you can't use a long string in a variant record.
If you need a string longer than 255 characters you have to devise a different structure, for example an array of char.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type TMp = packed record case integer of 0: (R: Real); 1: (S:array[0..1023] of AnsiChar); end;
Note in your case it is likely better to use real variants, not variant records.
F.e. the ms scripting engine works like that, with OleVariant types.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program vartest;{$apptype console}uses variants;var a:variant;begin a := 0.123; writeln(a); a := 'Testme'; writeln(a); readln;end.
Nafees:
One more problem, I used this code in Delphi to call functions in the script, but It won't work:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var Md: TMethod;begin Md.Code := Self.MethodAddress(MName); //Md.Code stays nil Md.Data := Pointer(Self); Result := TQScrFuncType(Md)(Args);end;
EDIT: I think it's because I need to place the {$M+} switch, but where?
EDIT2: Now it works, Had to place {$M+} after type.
Navigation
[0] Message Index