Delphi – Repeat Identifier of Attributes and Method Parameters

I transferred my project from Delphi to Lazarus. In a form, I have a private method with a parameter var Active: Boolean. It’s okay in Delphi, but Lazarus gives an error : Repeat the identifier “Activity” and prompt: the identifier has been defined in the FORMS in line 641, and in line 641 there are:

property Active: Boolean read FActive;

It is not difficult to change the parameter name (using refactoring), but why can’t I use the same name for method attributes and parameters?
In order to ensure that it is not an error of Delphi’s automatic conversion, I created a new project in Lazarus and added a private method

procedure Test(var Active: Boolean);< /pre> 

The result is the same. Even if I use const or nothing at all.
I checked the FPC documentation but didn't find any such restrictions. I'm just curious.

You should be able to use the same name for attributes and parameters. They have different scopes, so the one closest to the scope (parameters, Should be treated as being in the same scope as the local variable) should hide "further" in the scope (attribute). In Delphi, even in this method, you can still access the attribute, but you should limit it to Self .Active:

procedure TForm1.Test(var Active: Boolean);
var
ParamActive: Boolean;
FormActive: Boolean;
begin
ParamActive := Active; // gets the var parameter
FormActive := Self.Active; // gets the property
...
end ;

I don’t know why FPC marked it as an error. It shouldn’t.

Update

FWIW, if you change it

{$mode objfpc}

to

{$mode delphi}

It works as expected Compile, you won't receive an error. I just tried this.

I transferred my project from Delphi to Lazarus. In a form, I have a private Method, parameter var Active: Boolean. It doesn't matter in Delphi, but Lazarus gives an error error: duplicate identifier "active" and prompt: the identifier has been defined in the unit FORMS in line 641, and in line 641 there is:

property Active: Boolean read FActive;

It is not difficult to change the parameter name (using refactoring), but why can’t I use the same name for the method’s properties and parameters?
In order to ensure that it is not an error of Delphi’s automatic conversion, I created a new project in Lazarus and added a private method

procedure Test(var Active: Boolean);< /pre> 

The result is the same. Even if I use const or nothing at all.
I checked the FPC documentation, but didn't find any such restrictions. I'm just curious.

You should be able to use the same name for attributes and parameters. They have different scopes, so the one closest to the scope (parameters, should be treated as being in the same scope as the local variable) The "further" in the scope (attribute) should be hidden. In Delphi, even in this method, you can still access the attribute, but you should limit it to Self.Active:

procedure TForm1.Test(var Active: Boolean);
var
ParamActive: Boolean;
FormActive: Boolean;
begin
ParamActive := Active; // gets the var parameter
FormActive := Self.Active; // gets the property
...
end;

I don’t know why FPC marks it as an error. It shouldn't.

Update

FWIW, if you change it

{$mode objfpc }

to

{$mode delphi}

It compiles as expected and you won’t get an error. I just tried Pass this.

Leave a Comment

Your email address will not be published.