Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Where should procedures go

Status
Not open for further replies.

jjschwartz

Technical User
Feb 3, 2004
23
US
I'm having trouble understanding the scope of procedures and where they need to be declared.

Suppose I have a form with a button and a label and I have the OnClick event of the button set to change the label caption. Now suppose I want to write some procedure outside the event handler to help with this, so I change the event handler to:

procedure TForm1.ButtonClick1(..);
label1.caption := 'Label is changed';
helpwiththis;

If I put the following in the implementation section of the unit:

procedure helpwiththis;
begin
showmessage('it worked');
end;

it works fine.

If, however, I try to access the label:

procedure helpwiththis;
begin
label1.caption := 'it worked'
end

it doesn't work and label1 is an undeclared identifier.

On the other hand if I place "procedure helpwiththis;"
in the type declaration of TForm1, I can put

procedure TForm1.helpwiththis;
label1.caption := 'it worked';

in the implementation section and it works.

So I've gotten my whole project working by moving all my procedure declarations into the form's type declaration but I don't know if this is correct programming. When should a procedure be declared in the type declaration of the form? What about units without forms? When can a procedure just be written above the procedure that calls it? What happens when I want to move these helping functions into another unit. Will "uses helpingunit" do the trick?

Very wordy. TIA,
Jeff Schwartz

 
Your helpwiththis procedure makes a references to a component (label1) on your TForm1 so it makes sense to declare helpwiththis as a private or public procedure of TForm1.

You could have coded the helpwiththis procedure as follows:

procedure helpwiththis;
begin
Form1.label1.caption := 'it worked';
end;

but that wouldn't really show that helpwiththis is associated with Form1.

Andrew
Hampshire, UK
 
Actually, the correct way is making the procedure part of the form definition (what is called a "method"; methods and procedures are different in scope).

Code:
type
  TForm1 = class(TForm)
    ...
    private 
    procedure TForm1.HelpWithThis;  // method
    ...
  end;

The var Form1 will point to your form if and only if the form is automatically created. If the form is crated "in code", Form1 can be empty and the procedure will fire a memory access error. On the other way, a method will work with any instance of the form and not only with the instance pointed for Form1.

My best advice: try to get a book about OOP in Delphi; you'll need it.

buho (A).
 
Thanks much to buho and towerbase.

I'm a preferred customer at amazon.com because I've purchased what seems like every book, in and out of print, on learning OOP and Delphi. OOP is coming along, albeit slowly. I'm very open to suggestions on best books or best approaches.

Most of my books suggest that methods and procedures are the same thing. Can you elaborate?

Jeff Schwartz
 
May be another user can give you some advise on Delphi OOP books; being and old-timer Pascal programmer I've never buyed/read such a thing. I believe Cantu and Calvert's books are good, but I don't know if they pay attention too OOP concepts or assume the reader is used with them. The PDFs bounded in the CD compiler can give you some insight, but they are not a beginner read.

Regretabily, a real explanation about the proper use of methods will call for way more time I can spend, not to say that being my english laughable, I doubt anyone getting anything from it.

Sorry for not helping you here.

buho (A).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top