Forum > General

Error: Duplicate identifier XXX when method param has same name of variable

(1/6) > >>

Чебурашка:
Is it possible to avoid "Error: Duplicate identifier "XXX"" in definition of method SetValue. I forces me to call the parameter somehow differently _field, field_, fieldValue, and I don't like so much.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit ExampleUnit; {$mode objfpc} interface type    TExampleClass = class(TObject)  public    Field: string;    procedure SetValue(field: string);  end; implementation procedure TExampleClass.SetValue(field: string);begin  Self.Field := field;end; end. 
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---     procedure MethodExample(setvalue: string); implementation procedure TExampleClass.MethodExample(setvalue: string);begin  Writeln(setvalue);end;  
Thank you

Zvoni:
2 things:
1) Don't use fields in Public section. Use Property
2) Don't use "clear" Parameters-Names. It's easiest to follow FPC-Naming

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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

Чебурашка:

--- Quote from: Zvoni on November 29, 2022, 10:56:26 am ---2 things:
1) Don't use fields in Public section. Use Property
2) Don't use "clear" Parameters-Names. It's easiest to follow FPC-Naming

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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

--- End quote ---

Thanks for the suggestions.

Anyway I would stick to the original question, as the conventions are for sure fine, but are not part of the compiler.

KodeZwerg:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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 = setter
Example2 = no setter needed

Чебурашка:

--- Quote from: KodeZwerg on November 29, 2022, 11:19:47 am ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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 = setter
Example2 = no setter needed

--- End quote ---

This is a modification of the code according to a convention. And is perfectly fine.

But does not answer to my question.

Navigation

[0] Message Index

[#] Next page

Go to full version