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
dhertzfeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dhertzfeAuthor Commented:
Fantastic!!!   This makes my life much easier. Again thanks for the help.

Chad
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.