Recent

Author Topic: [SOLVED] Possibilities of TClass  (Read 8307 times)

yurkad

  • Full Member
  • ***
  • Posts: 173
  • development
[SOLVED] Possibilities of TClass
« on: April 17, 2017, 04:03:57 pm »
We have this function:
Code: Pascal  [Select][+][-]
  1. function getLabel(frmParent:TForm):TLabel;
  2. begin
  3.   Result := TLabel.Create(frmParent);
  4.   Result.Parent := frmParent;
  5. end;

Can there be something like this?
O how to make this function correct?
Code: Pascal  [Select][+][-]
  1. function getComponent(clsType:TClass; frmParent:TForm):TComponent;
  2. begin
  3.   Result := clsType.Create;
  4.   Result.Parent := frmParent;
  5. end;
« Last Edit: April 18, 2017, 01:01:29 am by yurkad »

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: Possibilities of TClass
« Reply #1 on: April 17, 2017, 06:31:06 pm »
Something like:
Code: Pascal  [Select][+][-]
  1. function getComponent(clsType:TComponentClass; frmParent:TComponent):TComponent;
  2. begin
  3.   Result := clsType.Create(frmParent);
  4. end;
  5.  
Note for this code to work properly, the component you are trying to create must also be registered!
Specialize a type, not a var.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: Possibilities of TClass
« Reply #2 on: April 17, 2017, 06:40:20 pm »
Can there be something like this?

Almost.  TClass is just a general pupose "class of TObject".  However, the constructor you are trying to call is inherited from TComponent, and the Parent property is inherited from TControl, so you need to change TClass to TControlClass (aka "class of TControl") instead:

Code: Pascal  [Select][+][-]
  1. function getControl(clsType: TControlClass; frmParent: TForm): TControl;
  2. begin
  3.   Result := clsType.Create(frmParent);
  4.   Result.Parent := frmParent;
  5. end;

Then you can call it with any TControl-derived class type, eg:

Code: Pascal  [Select][+][-]
  1. lbl := TLabel(getControl(TLabel, myForm);
« Last Edit: April 17, 2017, 06:44:14 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: Possibilities of TClass
« Reply #3 on: April 17, 2017, 06:43:48 pm »
Note for this code to work properly, the component you are trying to create must also be registered!

No, it doesn't.  That is true for DFM/LFM streaming, but not when you are passing in the actual class type as an input parameter.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

yurkad

  • Full Member
  • ***
  • Posts: 173
  • development
Re: Possibilities of TClass
« Reply #4 on: April 17, 2017, 09:30:27 pm »
Thaddy, Remy Lebeau  - Thanks!

I am beginning to love lazarus.

Can be use
Code: Pascal  [Select][+][-]
  1. lbl := clsType(getControl(TLabel, myForm));  
instead
Code: Pascal  [Select][+][-]
  1. lbl := TLabel(getControl(TLabel, myForm));
?
« Last Edit: April 17, 2017, 09:43:16 pm by yurkad »

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: Possibilities of TClass
« Reply #5 on: April 18, 2017, 08:59:30 pm »
Note for this code to work properly, the component you are trying to create must also be registered!

No, it doesn't.  That is true for DFM/LFM streaming, but not when you are passing in the actual class type as an input parameter.
Yes it is... these idiots want to try to be smart and save on compile size....
You should know that!

So: NO you first need to have a registered class anywhere, otherwise it fails. OMG. Even Remy.... (maybe you're getting old too...)
Specialize a type, not a var.

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: [SOLVED] Possibilities of TClass
« Reply #6 on: April 18, 2017, 09:19:14 pm »
Uhh, no. You do not need class to be registered in this case. When you are using RTL serializing functions, then you need register them so it can find correct class type and create proper instance.

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: [SOLVED] Possibilities of TClass
« Reply #7 on: April 18, 2017, 09:21:48 pm »
Uhh, no. You do not need class to be registered in this case. When you are using RTL serializing functions, then you need register them so it can find correct class type and create proper instance.
Uhh... no... try that at RUNTIME.... >:D >:D >:D >:D >:D >:D Oh, well, I need a Whatever, but it doesn't work.... Simple minds.
Specialize a type, not a var.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: [SOLVED] Possibilities of TClass
« Reply #8 on: April 18, 2017, 09:27:53 pm »
Thaddy, Remy is completely correct.
Try this unregistered test control:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Forms, Controls;
  9.  
  10. type
  11.  
  12.   TTest = class(TCustomControl)
  13.   protected
  14.     procedure Paint; override;
  15.   end;
  16.  
  17.   TForm1 = class(TForm)
  18.     procedure FormCreate(Sender: TObject);
  19.   private
  20.     FTest: TTest;
  21.   public
  22.  
  23.   end;
  24.  
  25.   function CreateControl(aControlClass: TControlClass; anOwnerParentForm: TForm): TControl;
  26.  
  27. var
  28.   Form1: TForm1;
  29.  
  30. implementation
  31.  
  32. function CreateControl(aControlClass: TControlClass; anOwnerParentForm: TForm): TControl;
  33. begin
  34.   Result:=aControlClass.Create(anOwnerParentForm);
  35.   Result.Parent:=anOwnerParentForm;
  36. end;
  37.  
  38. {$R *.lfm}
  39.  
  40. procedure TTest.Paint;
  41. var
  42.   r: TRect;
  43. begin
  44.   r:=ClientRect;
  45.   Canvas.Brush.Color:=Color;
  46.   Canvas.Rectangle(r);
  47.   Canvas.TextOut(35, 42, Caption);
  48. end;
  49.  
  50. procedure TForm1.FormCreate(Sender: TObject);
  51. begin
  52.   FTest:=TTest(CreateControl(TTest, Form1));
  53.   FTest.SetInitialBounds(20, 20, 100, 100);
  54.   FTest.Caption:='TTest';
  55. end;
  56.  
  57. end.

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: [SOLVED] Possibilities of TClass
« Reply #9 on: April 18, 2017, 10:02:23 pm »
Thaddy, Remy is completely correct.
Of course not. There is nothing linked in your program that is NOT registered. Remy will confirm that.
If you try to make a button at run-time and the button has not been used it does not work. But anyway. you're not a bright light - yet.

IF a class (component ) is used... anywhere else.... it works..... If not, it fails..
 BC (That's short for poultry with their heads chopped off)  >:D :D
« Last Edit: April 18, 2017, 10:04:17 pm by Thaddy »
Specialize a type, not a var.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: Possibilities of TClass
« Reply #10 on: April 19, 2017, 08:02:41 pm »
Can be use
Code: Pascal  [Select][+][-]
  1. lbl := clsType(getControl(TLabel, myForm));  
instead
Code: Pascal  [Select][+][-]
  1. lbl := TLabel(getControl(TLabel, myForm));
?

No.  You have to type-cast to a concrete type, you can't cast using a metaclass variable whose value is not known at compile-time.

The real question is, why are you creating controls dynamically in this fashion (using metaclass types) to begin with?
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: [SOLVED] Possibilities of TClass
« Reply #11 on: April 19, 2017, 08:08:08 pm »
Of course not. There is nothing linked in your program that is NOT registered. Remy will confirm that.

I DO NOT confirm what you have said.  If you have a specific component type defined in code, you CAN create instances of that class directly (via metaclass or otherwise), it DOES NOT need to be registered first.  Registering a class only applies to serialization, not to instantiation.  The serialization framework needs to be able to read a class name and find the registered class type for that name.  After that, registration is not used to actually instantiate an instance of that class type.

Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: [SOLVED] Possibilities of TClass
« Reply #12 on: April 19, 2017, 09:56:42 pm »
Are you dumb...

No.

IF you compile a simple program with just a single form and a memo...
You expect a treeview or a listview to magically appear when idiots like you or OP want that?

TONE DOWN THE ATTITUDE!  There is no need to be disrespectful.

Of course not! The code needs to be linked in... Which means the class needs to be registered...

The desired class simply needs to be DECLARED (usually by adding its implementing unit to your 'uses' clause), and its PPU (if any) linked into the final executable.  But the class does not need to be REGISTERED in order to be usable.  DECLARING a class and REGISTERING a class are COMPLETELY DIFFERENT THINGS.  Registration only applies to serialization, and what the OP is attempting to do does NOT involve serialization, so registration is NOT needed.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Tomas Hajny

  • Moderator
  • New Member
  • *****
  • Posts: 45
Re: [SOLVED] Possibilities of TClass
« Reply #13 on: April 19, 2017, 10:21:31 pm »
Could people involved in a forum called "Beginners" either choose their language accordingly and focus facts rather than opinions of others, or stop posting to this forum, please? Thanks in advance.

yurkad

  • Full Member
  • ***
  • Posts: 173
  • development
Re: [SOLVED] Possibilities of TClass
« Reply #14 on: April 19, 2017, 11:06:39 pm »
Remy Lebeau:
Quote
The real question is, why are you creating controls dynamically in this fashion (using metaclass types) to begin with?

I like the same word META.
Using of things related to this word (in programation and out of programation) always opens new possibilities and makes it possible to do things on a regular basis.
I like Delphi and Lazarus just because here exist something of this.
In this project I still do not use metaclass types, but I will use this.

 

TinyPortal © 2005-2018