Franz
Franz

Reputation: 2049

2 units with same function. Calling one or the other depending on unit order in uses clause without qualifying the unit name?

My demo has two units, UnitA and UnitB, each containing a function called MyFunction.

UnitA.pas

unit UnitA;

interface

function MyFunction: string;

implementation

function MyFunction: string;
begin
  Result := 'Function from UnitA';
end;

end.

UnitB.pas

unit UnitB;

interface

function MyFunction: string;

implementation

function MyFunction: string;
begin
  Result := 'Function from UnitB';
end;

end.

In my main program or another unit, I can specify which MyFunction to call by using the unit name as a prefix.

MainProgram.pas

program MainProgram;

uses
  UnitA, UnitB;

begin
  // Calling MyFunction from UnitA
  WriteLn(UnitA.MyFunction);
  // Calling MyFunction from UnitB
  WriteLn(UnitB.MyFunction);
  ReadLn;
end.

I remember I can directly call MyFunction, without any unit prefix, and Delphi will use the function from the last unit written in the uses clause. But this does not work for me using Delphi 11.0.

Did something change, or is my understanding not correct?

Upvotes: 2

Views: 102

Answers (0)

Related Questions