2 things:1) Don't use fields in Public section. Use Property2) Don't use "clear" Parameters-Names. It's easiest to follow FPC-NamingCode: Pascal [Select][+][-]type TExampleClass = class(TObject) private FField:String; public Property Field: string Read FField Write FField; procedure SetValue(AField: string); //"AField" is FPC-naming end; As to your Question: Probably scoping-collision
Code: Pascal [Select][+][-]type TExample1 = class(TObject) strict private FField: string; private procedure SetField(const AField: string); public property Field: string read FField write SetField; // here we have a setter end; TExample2 = class(TObject) strict private FField: string; public property Field: string read FField write FField; // here we direct access end; implementation procedure TExample1.SetField(const AField: string);begin FField := AField;end;Example1 = setterExample2 = no setter needed
Is it possible to avoid "Error: Duplicate identifier "XXX"" in definition of method SetValue.
I beg future answer not to tell about properties/fields in public scope.This is a convention, a best practice for OOP.But compiler is another thing, and it allows fields in public scope.
Now your question is answered I hope.
BTW I note that it doesn't raise the same error if there is a method parameter with the same name as another method (also in this case should be a duplicate identifier?), it seems that is focused on variables mostly:Code: Pascal [Select][+][-] procedure MethodExample(setvalue: string); implementation procedure TExampleClass.MethodExample(setvalue: string);begin Writeln(setvalue);end; Thank you
So why would that not work, have any guess?
Quote from: KodeZwerg on November 29, 2022, 11:37:04 amSo why would that not work, have any guess?In Delphi the code I presented compiles successfully. This is the reason why I am asking.And, also, in Delphi, the code you presented compiles successfully.See attahced pictures in D7.
because Delphi allows it.if i insert a {$mode delphi} in my code above, it compiles, too, in Lazarus
Bottom Line: Any identifier (Field, Method, Property) within a class can only occur once, except overloads for Methods.It has to do with Scope of identifier