Search code examples
delphidebuggingdelphi-7

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?


Solution

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