jjschwartz
Technical User
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
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