Lazarus

Programming => General => Topic started by: coolmyf on May 29, 2019, 05:23:53 am

Title: Duplicate Identifier
Post by: coolmyf on May 29, 2019, 05:23:53 am
I wrote a simple program as below. When I compile it, the Lazarus compiler gives "Duplicate Identifier 'Name' Error".But the same code can be compiled smoothly in Delphi XE10. Could anyone tell me why?Thanks.

program Project1;

type

  { TMyobj }

  TMyobj=class
    public
      Name:string;
      constructor Create(Name:string);
      procedure writename;
  end;

{ TMyobj }

constructor TMyobj.Create(Name: string);
begin
  self.Name:=Name;
end;

procedure TMyobj.writename;
begin
   writeln(name);
end;

var MyObj:TMyobj;
begin
  MyObj:=TMyObj.Create('Mark');
  MyObj.writename;
  MyObj.Free;
  readln;
end.
Title: Re: Duplicate Identifier
Post by: Awkward on May 29, 2019, 06:10:48 am
try to use {$MODE DELPHI}
or check {$MODESWITCH DUPLICATELOCALS-} macro
but i prefer to use method headers like this:
constructor TMyobj.Create(aName: string);
i.e. not to use arguments and locals like class fields
Title: Re: Duplicate Identifier
Post by: coolmyf on May 29, 2019, 06:35:52 am
Thanks.
I add {$mode delphi}. It works.
Title: Re: Duplicate Identifier
Post by: ASerge on May 29, 2019, 02:39:04 pm
I add {$mode delphi}. It works.
It is better to change the name of the parameter, and even put the modifier "const" before it.