Andris Gauračs
Andris Gauračs

Reputation: 418

procedure from another unit in delphi shows undeclared identifier

I just can't seem to link a procedure from another unit to work in the main unit's form. I tried adding the procedure declaration below interface, as mentioned in this question How to run procedure from another unit? , but it didn't work. It keeps showing [DCC Error] Main.pas(27): E2003 Undeclared identifier: 'sayHi' Here are the codes for both units: Main.pas:

unit Main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Unit2;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
sayHi();
end;

end.

and Unit2.pas

unit Unit2;

interface uses Dialogs;

procedure sayHi();

implementation

procedure sayHi();
begin
  ShowMessage('hi');
end;

end.

Here's the dpr file for the project:

program gl;

uses
  Vcl.Forms,
  Main in 'Main.pas' {Form1},
  Unit2 in 'Unit2.pas';

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

Here is the main.dfm file:

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 444
  ClientWidth = 621
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
end

Upvotes: 1

Views: 6538

Answers (2)

SamAct
SamAct

Reputation: 539

This can also happen if you have forgot to define the procedure in the interface section at the top of the page.

Upvotes: 0

Robert Love
Robert Love

Reputation: 12581

I have seen this before, and it's always related to having a different version "Unit2" being found first.

You have more than one Unit2.dcu or pas on your machine.
The Unit2 that is being found without "SayHi" is being found first.

Please check your project and Delphi Global library paths.

Upvotes: 9

Related Questions