Recent

Author Topic: [SOLVED] Component name  (Read 3515 times)

Pe3s

  • Hero Member
  • *****
  • Posts: 533
[SOLVED] Component name
« on: January 14, 2022, 01:05:02 pm »
Hello, how can I pass a component name as a variable or constructor?
Code: Pascal  [Select][+][-]
  1. procedure(componentName :)
« Last Edit: January 14, 2022, 05:38:38 pm by Pe3s »

AlexTP

  • Hero Member
  • *****
  • Posts: 2401
    • UVviewsoft
Re: Component name
« Reply #1 on: January 14, 2022, 01:18:40 pm »
Youtube has videos for 'Pascal tutorial' search; you need them.

RayoGlauco

  • Full Member
  • ***
  • Posts: 179
  • Beers: 1567
Re: Component name
« Reply #2 on: January 14, 2022, 01:33:54 pm »
Pe3s, are you talking about passing a string (a component name) as a constructor (a procedure)?
This is nosense.
Yo need to explain this better.
To err is human, but to really mess things up, you need a computer.

balazsszekely

  • Guest
Re: Component name
« Reply #3 on: January 14, 2022, 01:40:56 pm »
Most likely OP wants to pass a component to another class. Use the constructor, like this:
Code: Pascal  [Select][+][-]
  1. type
  2.   TMyClass = class
  3.   private
  4.     FForm: TForm;
  5.   public
  6.     constructor Create(AForm: TForm);
  7.   end;
  8.  
  9. //..
  10.  
  11. constructor TMyClass.Create(AForm: TForm);
  12. begin  
  13.   FForm := AForm;
  14.   //use FForm, you don't have to free it on destructor
  15. end;
  16.  
  17.  

Thaddy

  • Hero Member
  • *****
  • Posts: 14367
  • Sensorship about opinions does not belong here.
Re: Component name
« Reply #4 on: January 14, 2022, 01:45:03 pm »
Pe3s, are you talking about passing a string (a component name) as a constructor (a procedure)?
This is nosense.
Yo need to explain this better.
Well, the constructor can be done to some extend, but OP should mind that FPC is not a scripting language.
It is unusual to use such constructs.
The technique to use is to use {$if declared(variableName)} in combination with RTTI to get a class reference from which you can create an instance. Highly NOT recommended, but that would work, I guess.
« Last Edit: January 14, 2022, 01:48:15 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Component name
« Reply #5 on: January 14, 2022, 02:45:54 pm »
Well, the constructor can be done to some extend, but OP should mind that FPC is not a scripting language.
It is unusual to use such constructs.
The technique to use is to use {$if declared(variableName)} in combination with RTTI to get a class reference from which you can create an instance. Highly NOT recommended, but that would work, I guess.
"the technique to use ... highly NOT recommended" seems like a self-contradiction.
RTTI is currently only available via FPC for published properties of classes and a few other limited cases.
What is so unusual about passing data as parameter(s) in a constructor? You make it sound as if such a technique were unsound or hackish. It is a perfectly acceptable way to pass data that you don't want exposed globally.

Pe3s

  • Hero Member
  • *****
  • Posts: 533
Re: [SOLVED] Component name
« Reply #6 on: January 14, 2022, 05:38:45 pm »
Thank you. greetings

PascalDragon

  • Hero Member
  • *****
  • Posts: 5469
  • Compiler Developer
Re: Component name
« Reply #7 on: January 14, 2022, 08:44:46 pm »
Well, the constructor can be done to some extend, but OP should mind that FPC is not a scripting language.
It is unusual to use such constructs.
The technique to use is to use {$if declared(variableName)} in combination with RTTI to get a class reference from which you can create an instance. Highly NOT recommended, but that would work, I guess.
"the technique to use ... highly NOT recommended" seems like a self-contradiction.
RTTI is currently only available via FPC for published properties of classes and a few other limited cases.
What is so unusual about passing data as parameter(s) in a constructor? You make it sound as if such a technique were unsound or hackish. It is a perfectly acceptable way to pass data that you don't want exposed globally.

I think the “highly NOT recommended” is related to Thaddy's idea of using {$if declared(variableName)}. The idea of using RTTI would of course be alright if the extended RTTI would already be integrated. ;)

Akira1364

  • Hero Member
  • *****
  • Posts: 561
Re: [SOLVED] Component name
« Reply #8 on: January 14, 2022, 10:50:33 pm »
Can you give an example of what you're actually trying to do?

 

TinyPortal © 2005-2018