• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 163
  • Last Modified:

Declaring Global Functions

Hi All,

Here is a quick 50 points for you.

I placed a function declaration in Unit1 in the PUBLIC area. I placed a reference to Unit1 in the USES area at the top of Unit5. Why do I get "Undeclared Identifier" error pointing to the function call in Unit5? Could you tell me the correct way of declaring functions so they are available to all of my Units?

                      Thanks in Advance
                      Chad
0
dhertzfe
Asked:
dhertzfe
  • 3
  • 2
1 Solution
 
intheCommented:
hi,
like so works ok calling like
form1.somefunction():

Regards Barry


form1:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure foo(s : string);
  end;

var
  Form1: TForm1;

implementation

uses Unit2;

{$R *.DFM}

procedure Tform1.foo(s : string);
begin
showmessage(S);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
foo('hello from form1');
form5.show;
end;

end.





form5:

unit Unit5;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm5 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form5: TForm5;

implementation

uses Unit1;

{$R *.DFM}

procedure TForm5.Button1Click(Sender: TObject);
begin
form1.foo('hello from form5');
end;

end.
0
 
DrDelphiCommented:
OR you could have a seperate unit that contains only Globals (variables, functions, etc). I use one regularly for functions that I use on a day to day basis, rather than copying and pasting all the time. This way you can also be sure never to run into any circular references. Food for thought. Good luck!

 
0
 
intheCommented:
yep  i also have a seperate unit that contains my most often used functions,then i can just add it to the forms main uses section whenever i need it.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
dhertzfeAuthor Commented:
Hi Barry,

Thanks for the answer. send answer so I can award the points to you. I did the frmName.FunctionName but I typed it as frm.Name.FunctionName.  I had an extra period.

If I use a separate unit to store all my functions, how would I reference the function in my other units?
         UnitGlobal.FunctionName


Thanks to you and DrDelphi,
Chad
0
 
intheCommented:
ok cheers,
for a functio unit you make a new unit and add some functions roughly like:

unit function_unit;

interface

uses windows,dialogs;

procedure foo(s : string);

implementation

procedure foo(s : string);
begin
showmessage(s);
end;

end.


now save unit and add unit to library path or put where it can be found(delphi\lib for instance)

then make a new project and add the function_unit to the uses section like :


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls,function_unit;

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

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
foo('some text');
end;

end.

hope that helps some ;-)
0
 
dhertzfeAuthor Commented:
Fantastic!!!   This makes my life much easier. Again thanks for the help.

Chad
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now