1
votes

I want to edit a property of a shape inside a procedure. However if I create my own procedure I get an "undefinded identifier" error.

I tried to edit the property in the OnCreate event procedure of my form and that works just fine.

Why is it like that and how can I fix it?

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls;

type
    Tfrm_main = class(TForm)            
    shp_wheelLeftInside: TShape;
    shp_wheelRightInside: TShape;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }

  public
    { Public declarations }
  end;

var
  frm_main: Tfrm_main;

implementation

{$R *.dfm}

procedure addWheelInsides();
begin

    shp_wheelRightInside.Height := 42;           //this is where the error occurs

end;

procedure Tfrm_main.FormCreate(Sender: TObject);
begin

   shp_wheelLeftInside.Height := 42;
   shp_wheelRightInside.Height := 42;

  addWheelInsides();

end;

end.
2
Why you don't pass the TShape on your proc? - Ilyes
The form does not know anything about your procedure ...add it : var frm_main: Tfrm_main; procedure addWheelInsides; implementation Yes, and you can like Sami said add TShape to your procedure. - user763539

2 Answers

4
votes

The problem is that shp_wheelRightInside is a field that belongs to your Tfrm_main class whereas the addWheelInsides() method you have declared as a naked, ordinary method that belongs to nothing. The method, therefore, does not have access to the fields which belong to the form.

One solution is to move the method, which intends to operate on objects owned by the form, into the form itself.

  Tfrm_main = class(TForm)            
    shp_wheelLeftInside: TShape;
    shp_wheelRightInside: TShape;
    procedure FormCreate(Sender: TObject);
  private
    procedure addWheelInsides();  {declare it here}    
  public
    { Public declarations }
  end;

Which you then implement as a method of the form class as :

procedure Tfrm_main.addWheelInsides();
begin    
  shp_wheelRightInside.Height := 42;   
end;
2
votes

The shp_wheelRightInside field is not visible in your procedure. Declare the procedure addWheelInsides() inside the form as a method instead to resolve the shp_wheelRightInside scope.

type
    Tfrm_main = class(TForm)            
    shp_wheelLeftInside: TShape;
    shp_wheelRightInside: TShape;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure addWheelInsides;

  public
    { Public declarations }
  end;

If you want to extend the procedure across several units, pass TShape as a parameter instead.