Recent

Author Topic: [SOLVED] The scope of component properties?  (Read 3005 times)

Elmug

  • Hero Member
  • *****
  • Posts: 849
[SOLVED] The scope of component properties?
« on: July 29, 2011, 01:01:37 am »
Hi good people,

On a straight forward project with only one form, I would appreciate verification on this:

1. For coding purposes, so far I view the properties of the components of a form as kind of GLOBAL variables. Except for exceptions. Is this correct?

2. Now, although I might be confused, it seems to me that sometimes I can refer to Label1.Caption directly, and sometimes the compiler wants Form1.Label1.Caption because the identifier is 'not found'.

Is there any guideline that someone would please provide, that clears my head on why/when one needs to use the "Form1." prefix and when not?

Thanks.
« Last Edit: July 29, 2011, 02:09:03 am by Elmug »

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: The scope of component properties?
« Reply #1 on: July 29, 2011, 01:23:10 am »
Lets assume that Label1 is on the Form1.
If you have method of Form1 then you can access Label1:
Code: [Select]
procedure TForm1.MyMethod;
begin
  Label1.Caption:='sgfgdfh';
......
If you have method of other class (or some stand-alone procedure out of any hierarchy) then you must access Label1 like this:
Code: [Select]
procedure MyProcedure;
begin
  Form1.Label1.Caption:='sdgsdgs';
..........
or
Code: [Select]
procedure TForm2.MyMethod;
begin
  Form1.Label1.Caption:='sgfgdfh';
......
In the second example must be Unit1 in 'uses' section of Unit2.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Elmug

  • Hero Member
  • *****
  • Posts: 849
Re: The scope of component properties?
« Reply #2 on: July 29, 2011, 02:08:32 am »
Thanks for the great help, Blaazen.

It makes good sense, although I will be studying this for a few days before I really is straight in my head.

I say I get it, though, and consider this solved.

 

TinyPortal © 2005-2018