Tutorial :Is there a function like PHP's vardump in Delphi?



Question:

I've given up on the Delphi 7 debugger and am pretty much relying on outputdebugstrings. Is there a standard function I can call to get the contents of an object as a string like the debugger would if I set a breakpoint?


Solution:1

Not exactly what your looking for, but you can use RTTI to get access to the values of various published properties. The magical routines are in the TypInfo unit. The ones you are probably most interested in are GetPropList which will return a list of the objects properties, and GetPropValue which will allow you to get the values of the properties.

procedure TForm1.DumpObject( YourObjectInstance : tObject );  var    PropList: PPropList;    PropCnt: integer;    iX: integer;    vValue: Variant;    sValue: String;  begin    PropCnt := GetPropList(YourObjectInstance,PropList);    for iX := 0 to PropCnt-1 do      begin        vValue := GetPropValue(YourObjectInstance,PropList[ix].Name,True);        sValue := VarToStr( vValue );        Memo1.Lines.Add(PropList[ix].Name+' = '+sValue );      end;  end;  

for example, run this with DumpObject(Self) on the button click of the main form and it will dump all of the properties of the current form into the memo. This is only published properties, and requires that the main class either descends from TPersistent, OR was compiled with {$M+} turned on before the object.

Rumor has it that a "reflector" like ability will be available in a future version of Delphi (possibly 2010).


Solution:2

Consider something like Codesite which is a much more complete tracing solution. It allows you to output much more complex info, and then search, print, and analyse the data. But for your purposes, you can simply send an object to it with Codesite.Send('Before', self); and you get all the RTTI available properties in the log. Do an "After" one too, and then you can compare the two in the Codesite output just by selecting both. It's saved me many times.


Solution:3

if delphi 7 is the .NET version, then you could do (some of) that with reflection. (not easy, but not terribly hard). if it's the normal, compiled thing, then it's a hard problem and the debugger is you best bet, apart from specialized printing functions/methods.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »