Search code examples
delphidelphi-10.2-tokyo

Debugging TValue containing a string


I'm having trouble to inspect the string value of a TValue variable while debugging. The hover menu doesn't show the string value and even adding it to the watch list seems tricky.

Given a very basic sample console application, like

program Project1;

uses
  System.SysUtils, System.Rtti;

var
  Value: TValue;
begin
  Value := 'Hello';
  WriteLn(Value.AsType<string>);
  ReadLn;
end.

Adding a breakpoint to the WriteLn line, I can hover over the Value variable and see the value type, but there isn't really any information on the string value, see

Variable in Debugger

Next thing I tried was adding it to the watch list, using .AsType<string> - however watchlist errors - Internal Error in the sample application, E2531 (method requires explicit type arguments) in my real application. It made no difference to check/uncheck the option to allow side effects and functions . Watch list error

What finally worked was creating a function for the conversion, e.g.

function ValueToString(const Value: TValue): string;
begin
  Result := Value.AsType<string>;
end;

and using this in the watchlist. I had to use this function in aplication code though (e.g. in the WriteLn call in the sample) to avoid it being removed by the linker. This also means that I'll have to add this funtion or a unit containing this function to every unit I want to debug.

Is there a better solution?


Solution

  • Using TValue's built-in Value.ToString works in a watch. Value.AsString also evaluates.