Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 619
Peter Turner Avatar asked May 28 '09 21:05

Peter Turner


People also ask

What is the use of Var_dump in PHP?

Introduction to the PHP var_dump function The var_dump () is a built-in function that allows you to dump the information about a variable. The var_dump () function accepts a variable and displays its type and value. Suppose that you have a variable called $balance with a value of 100:

How to dump a variable in PHP?

The var_dump () is a built-in function that allows you to dump the information about a variable. The var_dump () function accepts a variable and displays its type and value. Suppose that you have a variable called $balance with a value of 100: <?php $balance = 100;

How do I use VAR_dump and die in Python?

Use the var_dump () function to dump the information about a variable. Wrap the output of the var_dump () function in a pre tag to make the output more readable. The die () function terminates the script immediately. Combine var_dump () and die () functions to dump and die.

What is the value of $B in var_dump?

$b = "Hello world!"; The var_dump () function dumps information about one or more variables. The information holds type and value of the variable (s). var_dump ( var1, var2, ...); var1, var2, ...


2 Answers

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).

like image 172
skamradt Avatar answered Sep 22 '22 19:09

skamradt


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.

like image 27
mj2008 Avatar answered Sep 22 '22 19:09

mj2008