HOME - - - - - - - TUTORIALS INDEX - - - - - - - - - - - - Other material for programmers

Delphi: Simplify your life: Write routines once, use them in many applications

This tutorial is pretty rough around the edges, but was far enough along that I thought you'd want what's here. I'll try to revisit it, polish it, later.


Each of us has a personal style. Each of us is working with slightly different needs. We often have miscellaneous bits of code which we seem to put in many of our applications. There are various reasons to justify creating external units to hold those routines. Some of the reasons are good, some bad, but all are still valid!

With an external routine, you can "add words" to Delphi without the overheads of new components (the "best" way to do it). Once you have your external unit, most of the "hard work" is done. The next times you need whatever you've put in the external unit, it is merely a matter of including an entry in the new program's "uses" clause, and making available some files. You can get clever with compile paths, or simply put copies of the files in the folder holding the project using the external unit. what will you use external units for? I don't know... that's part of the beauty of the beasts: They let you adapt Delphi to your needs.

Your starting point is....
unit Unit2;

interface

implementation

end.
... which is what Delphi File|New|Unit produces.

As a first example, flesh that out as follows...
unit DD51ExtUnit;

interface

uses dialogs;

procedure SayHi;

implementation

procedure SayHi;
begin
showmessage('Hi from External Unit');
end;

end.
If you do it within Delphi, and hit SAVE, you'll be asked for a name for the unit. Call it DD51ExtUnit.pas, and project. Call project DD51.dpr. Save unit and project file in a folder set up for just this project.

Now create an application... File|New|Application

Add DD51ExtUnit to the uses clause.

Re-name the form as DD51f1

Put a button on it. Make the button's OnClick handler say SayHi, i.e....
procedure TDD51f1.Button1Click(Sender: TObject);
begin
SayHi;
end;
Save app, in same folder as other DD51 stuff, as DD51u1.pas. If you get "DD51.dpr exists. Overwrite?"... say yes.

Try to run. If you get "Undeclared identifier 'SayHi'", be sure you put DD51ExtUnit in the uses clause, didn't put a typo in the name- there, or while saving. It shouldn't happen! The program should run, bring up a form with a button. Clicking that should result in the message box appearing.

The "uses dialogs" is to give the compiler access to things it needs if you are to use showmessages.


Now for something more useful....

To the unit add.....
function IsEvenNumber(bTmp:byte):boolean;
... just after the FIRST instance of "procedure SayHi;", which is near the beginning, just after "implementation"

and add.......
function IsEvenNumber(bTmp:byte):boolean;
begin
if bTmp mod 2=0 then result:=true else result:=false;
end;
just before the "end." at the end of the unit.

Now add a label, an edit box, and a button to your main form (DD51f1).

Make the button's onclick handler say.....
procedure TDD51f1.Button2Click(Sender: TObject);
begin
if IsEvenNumber(strtoint(edit1.text)) then //no begin here
     label1.caption:='Even Number' // no ; here
     else label1.caption:='Odd Number';
end;
Now you can enter a number in the edit box, click the button, and get a report: Is the number odd or even?

This "application" needs to be tidied up... if you click button2 when there's an inappropriate string in the edit box, the application responds badly... but the cure is not too tedious, and would distract from the essential points of this tutorial. (If you want to fix the flaw, use the edit box's onchange event. Put a try... except structure in it.)


   Search this site or the web        powered by FreeFind
 
  Site search Web search
Site Map    What's New    Search


Click here if you're feeling kind! (Promotes my site via "Top100Borland")
Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows pc) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!

Click here to visit editor's freeware, shareware page.


Link to Tutorials main page
Here is how you can contact this page's author, Tom Boyd.


Valid HTML 4.01 Transitional Page WILL BE tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org


If this page causes a script to run, why? Because of things like Google panels, and the code for the search button. Why do I mention scripts? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....