Recent

Author Topic: Question about assignment  (Read 1341 times)

stab

  • Full Member
  • ***
  • Posts: 237
Question about assignment
« on: January 07, 2020, 01:25:14 pm »
In a library written FPC there is a property like:
  TClipper = class(TClipperBase)
  private
    FZFillCallback    : TZFillCallback;
  public
    property ZFillFunction: TZFillCallback read FZFillCallback write
       SetZFillCallback;
  end;

procedure TClipper.SetZFillCallback(AValue: TZFillCallback);
begin
  if not Assigned(FZFillCallback) then
    FZFillCallback:=AValue;
end;

When accessing SetZFillCallback I see that FZFillCallback is not
assigned and thus FZFillCallback:=AValue; is executed.
Investigating the assignment before and after reveals that  the value of FZFillCallback is not changed. HOW COME??? %)

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: Question about assignment
« Reply #1 on: January 07, 2020, 02:10:23 pm »
Shouldn't that be:

Code: Pascal  [Select][+][-]
  1. TClipper = class(TClipperBase)
  2. private
  3.   FZFillCallback    : TZFillCallback;
  4.   procedure SetZFillCallback(AValue: TZFillCallback);
  5. public
  6.   property ZFillFunction: TZFillCallback read FZFillCallback write
  7.        SetZFillCallback;
  8. end;
???

Otherwise, if the parent class TClipperBase (or any ancestor of it) has a SetZFillCallback(), it's that which will be executed (possibly setting other field or doing nothing at all)

Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

stab

  • Full Member
  • ***
  • Posts: 237
Re: Question about assignment
« Reply #2 on: January 07, 2020, 02:33:45 pm »
Sorry, I don't see any difference between my code and your suggestion. From a calling unit I see that procedure SetZFillCallback is entered but there isn't any change in the FZFillCallback procedure variable.

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: Question about assignment
« Reply #3 on: January 07, 2020, 05:54:42 pm »
Sorry, I don't see any difference between my code and your suggestion.

Your posted code lacks the declaration of the setter; if it's different in the real code then I don't know what may be happening, sorry.

Only one thing: If your code is in mode ObjFPC then maybe the setter should be like:

Code: Pascal  [Select][+][-]
  1. procedure TClipper.SetZFillCallback(const AValue: TZFillCallback);
  2. begin
  3.   if not Assigned(FZFillCallback) then
  4.     FZFillCallback := @AValue;
  5. end;
???

Though I'm not entirely sure of that ... :-[

Or maybe this?:

Code: Pascal  [Select][+][-]
  1. procedure TClipper.SetZFillCallback(AValue: TZFillCallback);
  2. begin
  3.   if FZFillCallback <> AValue then
  4.     FZFillCallback := AValue;
  5. end;
« Last Edit: January 07, 2020, 05:56:30 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

stab

  • Full Member
  • ***
  • Posts: 237
Re: Question about assignment
« Reply #4 on: January 07, 2020, 08:52:50 pm »
The problem is not that the code doesn't reach the assignment but that the assignment doesn't have any effect, i.e FZFillCallback is unaffected. %)

jamie

  • Hero Member
  • *****
  • Posts: 7594
Re: Question about assignment
« Reply #5 on: January 07, 2020, 11:00:07 pm »
Did you create the instance first ?

Also, in the constructor set the current value to NIL so that Assign will Fail and thus you will actually set the field.

 You may want to test for a NIL instead of using Assign in any case because it looks like this isn't a class or object method you are testing against..

 Also, callbacks when done from some outside of the control of the program should be done in a Class static address.

The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018