Two labels, one with statement, and the wrong caption

Go down

Two labels, one with statement, and the wrong caption

· #1

An old event handler uses with LeftLabel, RightLabel do and then assigns Caption. Both objects are labels. The right label changes even though I was reading the left name first.

The unit uses {$mode objfpc}. Would switching to {$mode delphi} change which label gets the assignment? I would prefer an explanation before I change the language setting of a working application over one noun.

Re: Two labels, one with statement, and the wrong caption

· #2

The last item has precedence. with A, B do behaves like nested with statements, with B inside A. If both expose Caption, the unqualified assignment resolves to B.Caption.

Switching between those modes does not fix this ambiguity. The with statement reference shows the lookup order.

Name the intended object explicitly. In a unit using StdCtrls, this routine changes only the left label:

pascal
procedure SetLeftStatus(LeftLabel, RightLabel: TLabel);
begin
  LeftLabel.Caption := 'Ready';
end;

The second parameter is unused here because the other label has no job in this operation. It can be removed when you update the callers.

Re: Two labels, one with statement, and the wrong caption

· #3

Both modes support classes. They differ in other syntax and compatibility rules, including how ordinary generic specializations are written. A mode directive applies to its source module, not automatically to every unit used by that module.

Choose the mode for the source you are maintaining. For this fault, remove the ambiguous lookup.

Re: Two labels, one with statement, and the wrong caption

· #4

Fixed with explicit names. The diff has more characters and fewer interpretations.

I checked the rest of the handler and found an unqualified Color as well. That explains the unusually cheerful panel.

Re: Two labels, one with statement, and the wrong caption

· #5

so the compiler picked a label and it was the one i wrote last

that is more organized than my method

Re: Two labels, one with statement, and the wrong caption

· #6

I once used nested with blocks to fit more code on a small monitor. The replacement monitor was cheaper than the time spent determining which Name each assignment meant.

Pete · thirty years of begin and end "It compiled on my machine."

Go up

Sign in to reply