Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set a breakpoint that triggers when a property is modified?

I have a public property with a read and write methods to a private field. I tried adding a data breakpoint for the field or property and I get an "Invalid address" error message. I qualified the variable name with the type name. Same Error. I also tried to put a regular breakpoint on the write method and the line turns green.

How do I break when the value of a variable changed in Delphi XE?

Update

This is a sample code:

Type
 TCustromer

private
 customerName: string;
public 
  property CustomerName: string  read customerName   write customerName;
end;

How do I break whenever CustomerName or customerName values change?

like image 490
Tony_Henrich Avatar asked Oct 16 '25 18:10

Tony_Henrich


1 Answers

You're coming from .Net, where the compilers replace direct-field property setters with stub methods. With that setup, I could understand why an IDE might let you put a breakpoint on the property declaration and interpret it as breakpoints in those hidden stub methods.

Delphi doesn't work that way, though. When a property declaration says it writes to a field, then assignments to the property assign directly to the field. There's no setter method to break in.

To detect writes to the property in your program, you could try to set data breakpoints. However, you'd have to do that for every instance of your class because each instance's field obviously lives at a different address. Furthermore, you'd have to do that anew every time you restarted your program because the instances' addresses wouldn't necessarily stay the same from one run to the next.

The easier way to accomplish your goal is simply to write a setter for your property. Then set a breakpoint in the implementation of that setter.

type
  TCustomer = class
  private
    FCustomerName: string;
    procedure SetCustomerName(const Value: string);
  public 
    property CustomerName: string read FCustomerName write SetCustomerName;
  end;

procedure TCustomer.SetCustomerName(const Value: string);
begin // Set breakpoint here.
  FCustomerName := Value;
end;
like image 81
Rob Kennedy Avatar answered Oct 18 '25 18:10

Rob Kennedy