Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

What am I doing wrong?

XybexXybex Member Posts: 2
[b][red]This message was edited by Xybex at 2005-6-10 12:8:51[/red][/b][hr]
OK, I am following SAMS "teach yourself delphi 4 in 21 days" book, and I have read chapter two. The author has set out some exercises, the first of which is something like "write a procedure to change a label caption and call it when a button is clicked." Sounds simple.

This is the unit code:

unit Unit1;

interface

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

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

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure change(x:string);
begin
Label1.caption := x;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
capt: string;
begin
capt:='hello';
change(capt);
end;

end.

I can't for the life of me find why the compiler doesn't like the procedure I put. It says "undeclared identifier Label1" but typing the exact same thing in an onclick event handler seems to work. What is the difference?
I would appreciate it if someone could help me out here.

I am using Delphi 7.

Comments

  • zibadianzibadian Member Posts: 6,349
    : [b][red]This message was edited by Xybex at 2005-6-10 12:8:51[/red][/b][hr]
    : OK, I am following SAMS "teach yourself delphi 4 in 21 days" book, and I have read chapter two. The author has set out some exercises, the first of which is something like "write a procedure to change a label caption and call it when a button is clicked." Sounds simple.
    :
    : This is the unit code:
    :
    : unit Unit1;
    :
    : interface
    :
    : uses
    : Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    : Dialogs, StdCtrls;
    :
    : type
    : TForm1 = class(TForm)
    : Label1: TLabel;
    : Button1: TButton;
    : procedure Button1Click(Sender: TObject);
    : private
    : { Private declarations }
    : public
    : { Public declarations }
    : end;
    :
    : var
    : Form1: TForm1;
    :
    : implementation
    :
    : {$R *.dfm}
    :
    : procedure change(x:string);
    : begin
    : Label1.caption := x;
    : end;
    :
    : procedure TForm1.Button1Click(Sender: TObject);
    : var
    : capt: string;
    : begin
    : capt:='hello';
    : change(capt);
    : end;
    :
    : end.
    :
    : I can't for the life of me find why the compiler doesn't like the procedure I put. It says "undeclared identifier Label1" but typing the exact same thing in an onclick event handler seems to work. What is the difference?
    : I would appreciate it if someone could help me out here.
    :
    : I am using Delphi 7.
    :
    The difference is the scope of the OnClick() and Change(). Label1 is a field of Form1. Fields are only accessible through either specifying which object they belong to or within other parts of the object itself. There are 2 ways to specify which object a field belongs to: using the dot (.)-symbol or the "with" reserved word:
    [code]
    Form1.Label1

    or

    with Form1 do
    Label1
    [/code]
    Inside methods (=procedures which are part of an object) all the fields and methods of that object are freely accessible. You might say that Delphi sees methods as:
    [code]
    procedure TForm1.Button1Click(Sender: TObject);
    var
    capt: string;
    begin
    [blue] with Form1 do
    begin[/blue]
    capt:='hello';
    change(capt);
    [blue] end;[/blue]
    end;
    [/code]
    The blue part is implicitly added by the compiler. In this case the method:
    [code]
    procedure TForm1.Button1Click(Sender: TObject);
    var
    capt: string;
    begin
    [blue] with Form1 do
    begin[/blue]
    Label1.Capion := 'Hello world';
    [blue] end;[/blue]
    end;
    [/code]
    is using the "with" word to specify, which object Label1 is part of.
    In the procedure Change() Delphi cannot be certain which object Label1 belongs to.
    Suppose you have 2 forms, each with a Label1. To which Label1 does the procedure Change() refer to? The one on Form1 or on Form2? The "human" error would read: "Undetermined object for field Label1". But the Delphi compiler tries to find the Label1 in the declared variables. Label1 isn't listed as a variable, thus it doesn't know that identifier. Hence that error.

    I hope this explains the nature of that error and also gives you a clue to the solution.
  • XybexXybex Member Posts: 2
    Thankyou. That helped immensely and it now works nicely. I thought I had the idea of scope down, but it looks like some more reading is in order on the subject.
    Thanks for taking the time to help me.
Sign In or Register to comment.