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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

My own procedure doesn't work!

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Why this code doesn't work?

Debugger:
[Error] Unit1.pas(29): Undeclared identifier: 'Label1'
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure MyProcedure;
begin
Label1.Caption := 'Hello!';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
MyProcedure;
end;

end.
 
The problem with the code that you have written is that the the variable Label1 is not visible to the procedure MyProcedure. You can solve this problem in two ways:

1. Use the Form1 variable as shown below.
procedure MyProcedure;
begin
Form1.Label1.caption := 'Hello';
end;

2. The second way is better as it a more of an OOP style. This gets you away from the old structured style of programming. I have listed the code below.

TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure MyProcedure;

public
{ Public declarations }
end;


var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.MyProcedure;
begin
Label1.caption := 'Hello!';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Myprocedure;
end;

HtH
regards

Long live Godess Athena
dArktEmplAr of Delphi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top