call function from another unit

Posted on 2010-04-05
Medium Priority
Last Modified: 2012-05-09
Dear Experts
 I have a function "SendEmail" that will be used in all my units.pas and I want to write that function only one time and call it from all the units, what should I do?

I tried  to put sendmail.pas in the uses statement in the calling unit but I got this error
"This form of method call only allowed for class methods"
thank you

Question by:Amanda77

Expert Comment

ID: 29876648
How do you call the function and how is it written? Here's an example how it should work: http://www.daniweb.com/forums/thread143363.html
if it still doesn't work the put a (simplified) version of your units online for us to see.... Maybe you are trying to call units.SendEmail or have you function placed inside a form or something.

Expert Comment

ID: 29877167
You probably add the method SendMail to a form or other class, and then try to call it directly without constructing the class.

Here's an example of a unit called Unit1, defining SendMail. Any other unit can put Unit1 in the uses clause and call the SendMail global function.
unit Unit1; // in unit1.pas

procedure SendMall;


procedure SendMail;
// ...


Open in new window

LVL 25

Accepted Solution

epasquier earned 2000 total points
ID: 29878209
you have called it from the class instead of from the instance (variable)
Your unit is like this :

   procedure SendEmail(SendTo,Subject,Body:String);


// Form1 is the variable containing the instance, TForm1 is the class.
// the instance is created with Create and stored in the variable somewhere in your application
// or in your project main source code
Application.CreateForm(TForm1, Form1);  

and you called from your unit :

TForm1.SendEmail('you@mail.com','test','email test');

instead of

Form1.SendEmail('you@mail.com','test','email test');

Author Closing Comment

ID: 31711237
thank you very much
that is exactly where I was mistaken...I guess I don't know that much about classes and instances

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
Suggested Courses
Course of the Month5 days, 22 hours left to enroll

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question