?
Solved

call function from another unit

Posted on 2010-04-05
4
Medium Priority
?
515 Views
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


   
0
Comment
Question by:Amanda77
4 Comments
 
LVL 8

Expert Comment

by:pvlier
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.
0
 
LVL 7

Expert Comment

by:ebob42
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
interface

procedure SendMall;

implementation

procedure SendMail;
begin
// ...
end;

end.

Open in new window

0
 
LVL 25

Accepted Solution

by:
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 :

Type
 TForm1=class(TForm)
...
 public
   procedure SendEmail(SendTo,Subject,Body:String);
 end;

Var
 Form1:TForm1;

// 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
Form1:=TForm1.Create(Application.MainForm);
// 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');
0
 

Author Closing Comment

by:Amanda77
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
0

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