Morfik Tip: How to return the value of any class member field as a string
February 14, 2007We are building a generic framework to do data transfers/data binding efficiently (and some other wonders;) in Morfik and we had this issue today that we wanted to get a field’s value when we knew the field’s name.
So we cooked up the following little Pascal function that will return the string equivalent of the value of filed ‘v’ of class ‘c’. This is a first cut, some tests should be done (and will be for our production lib) but it is a good start, if you happen to stumble upon the same problem.
function GetValue(c:Pointer; v:String):String; JavaScript; (*!
{
return c[v];
}*)
Among other things this demonstrates how you can call JavaScript code from within the Pascal code without using external .js files. This is a relatively new syntax (from around the 0.9.1x.x branch AFAIS) but is way cool.
If you happen to have to drop to JavaScript, some of my advices:
- do NOT do that, unless you know what you are doing — if you are new to JavaScript, it will be an interesting experience;
- in Project / Project Options / Compiler, switch off “Obfuscate JavaScript” (and “Optimize JavaScript”, for that matter) to make your life easier when you are debugging;
- install the Microsoft Script debugger as described in Appendix ‘G’ (if I recall correctly).
Happy sailing!
