Bixby Sayz
2014-02-28 09:29:55
Ran into this nasty "gotcha" using Lape:
If the name of the parameters in the function/procedure match the names of properties of the datatype the the values passed in are ignored. Holy Crap! That took me three very painful hours to figure out what was going on. The code looked right but didn't behave as expected. Can you say "We need a debugger"? I knew you could."
program new;
type
TMyType = record
X: Integer;
Y: Integer;
end;
procedure TMyType.Assign(const X, Y: Integer);
begin
WriteLn(Format('Assign Params: %d, %d.', [X, Y]));
Self.X := X;
Self.Y := Y;
end;
procedure TMyType.Assign2(const XValue, YValue: Integer);
begin
WriteLn(Format('Assign2 Params: %d, %d.', [XValue, YValue]));
Self.X := XValue;
Self.Y := YValue;
end;
var
MyVar: TMyType
begin
MyVar.X := 1; MyVar.Y := 2;
WriteLn(Format('MyVar - Before Assign: %d, %d.', [MyVar.X, MyVar.Y]));
WriteLn('>> Passing 3,4 to Assign()');
MyVar.Assign(3, 4);
WriteLn(Format('MyVar - After Assign: %d, %d.', [MyVar.X, MyVar.Y]));
WriteLn('>> Passing 5,6 to Assign2()');
MyVar.Assign2(5, 6);
WriteLn(Format('MyVar - After Assign2: %d, %d.', [MyVar.X, MyVar.Y]));
end.MyVar - Before Assign: 1, 2.
>> Passing 3,4 to Assign()
Assign Params: 1, 2.
MyVar - After Assign: 1, 2.
>> Passing 5,6 to Assign2()
Assign2 Params: 5, 6.
MyVar - After Assign2: 5, 6.
Successfully executed.
If the name of the parameters in the function/procedure match the names of properties of the datatype the the values passed in are ignored. Holy Crap! That took me three very painful hours to figure out what was going on. The code looked right but didn't behave as expected. Can you say "We need a debugger"? I knew you could."