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