Recent

Author Topic: GUI (control) elements are public by default?  (Read 1170 times)

OdysseusL

  • New member
  • *
  • Posts: 7
GUI (control) elements are public by default?
« on: June 20, 2023, 12:26:18 am »
A very basic question: Building a window with TForm and adding various control elements (button, text fields, ...) ... Are these control elements "public" by default? So they may accessed from every code outside of the object?

If yes does this mean that every "external" code may manipulate the control elements via the accessable methods?

Is it not better to make them private? Do I miss something?

Thanks in advance.

jamie

  • Hero Member
  • *****
  • Posts: 7603
Re: GUI (control) elements are public by default?
« Reply #1 on: June 20, 2023, 01:44:51 am »
They are not public to the form. They don't become a member of the form if that is what you mean ?

However, they are more than likely in the component list of the owned controls/components of the form for which you can search them.
The only true wisdom is knowing you know nothing

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: GUI (control) elements are public by default?
« Reply #2 on: June 20, 2023, 01:53:52 am »
A very basic question: Building a window with TForm and adding various control elements (button, text fields, ...) ... Are these control elements "public" by default? So they may accessed from every code outside of the object?
Yes, correct.

Quote
If yes does this mean that every "external" code may manipulate the control elements via the accessable methods?
Yes, correct.

Quote
Is it not better to make them private? Do I miss something?
Why ? No, you did not miss something. The question is why on a (public) form would there be a need to make the components that are placed on it private ?

As Jamie indicated they are accessible using the forms controls property anyway (see for example https://forum.lazarus.freepascal.org/index.php/topic,63704.0.html)
« Last Edit: June 20, 2023, 01:55:59 am by TRon »
Today is tomorrow's yesterday.

cdbc

  • Hero Member
  • *****
  • Posts: 2686
    • http://www.cdbc.dk
Re: GUI (control) elements are public by default?
« Reply #3 on: June 20, 2023, 07:11:57 am »
Hi
They're actually "published" not "public". The reason for that, we shall find in ...Tadaaaa: Rtti, Run Time Type Information, which is used in the streaming mechanism of the form and controls on it and the reason, you can design your forms with the Object Inspector. If you look closer, you'll see that all the visual elements descend from TPersistent, which is compiled with {$M+} that turns on embedded typeinfo, BUT only for things in the "published" section, that was then (standard TypeInfo), a new extended Rtti is in the forge, to be able to query all members of a class / interface. And by convention the elements in the "sectionless" section of a class are "published". If you want to tap into the power of Rtti, the units (TypInfo & Rtti) are there for you to play with, it's fun.
If you make the class elements private, even Rtti can't touch them, I tried  ;D
Regards Benny
« Last Edit: June 20, 2023, 07:20:07 am by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

OdysseusL

  • New member
  • *
  • Posts: 7
Re: GUI (control) elements are public by default?
« Reply #4 on: June 21, 2023, 12:11:09 am »
Thank you very much for all your replies. This helps!

 

TinyPortal © 2005-2018