Recent

Author Topic: Access Self from With statement  (Read 1283 times)

GreatCorn

  • New Member
  • *
  • Posts: 40
    • GreatCorn
Access Self from With statement
« on: July 27, 2020, 03:20:33 pm »
So I'm programming with TComponents and in my case, the TComponent's... type can be specified by the user. Right now I'm trying to find a way to create a form. I have a TComponent variable (let's say component), and using
Code: Pascal  [Select][+][-]
  1. with component as TForm do
lets me configure the form, but I am unable to pass the variable itself as a TForm to Application.CreateForm. Using Self isn't allowed there and I just can't figure anything out...

MarkMLl

  • Hero Member
  • *****
  • Posts: 8572
Re: Access Self from With statement
« Reply #1 on: July 27, 2020, 03:43:34 pm »
Can't be done. You have to repeat yourself.

There's been sporadic discussion of introducing a placeholder so that the referent of with is accessible by name, but they've not got anywhere due to hostility to the with construct and the fact that with can both cause the compiler to do unexpected things and make it very difficult for the source to be followed.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

BeniBela

  • Hero Member
  • *****
  • Posts: 959
    • homepage
Re: Access Self from With statement
« Reply #2 on: July 27, 2020, 04:42:53 pm »
This works:

Code: Pascal  [Select][+][-]
  1. {$ModeSwitch typehelpers}      
  2.  
  3. type TFormHelper = type helper for TForm
  4.   function Self: TForm;
  5. end;
  6.  
  7. function TFormHelper.Self: TForm;
  8. begin
  9.   result := self
  10. end;
  11.  
  12.  
  13. procedure x(f: TComponent);
  14. begin
  15.   with f as TForm do begin
  16.     self.caption := 'x';
  17.   end;
  18. end;        
  19.  

Not sure how stable it is

Warfley

  • Hero Member
  • *****
  • Posts: 2056
Re: Access Self from With statement
« Reply #3 on: July 27, 2020, 05:03:37 pm »
For such things I simply use a (nested) function:
Code: Pascal  [Select][+][-]
  1. procedure DoStuffWithTheForm(AForm: TForm);
  2. ...
  3.  
  4.   if component is TForm then DoStuffWithTheForm(TForm(component));

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Access Self from With statement
« Reply #4 on: July 27, 2020, 05:05:38 pm »
Hi!

That is the way for your strange desire :

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2.  var Form2 :   TForm1;
  3.  c : TComponent;
  4. begin
  5. Application.CreateForm(TForm1, TForm(c));
  6. TForm(c).show;
  7. end;
  8.  

Winni

GreatCorn

  • New Member
  • *
  • Posts: 40
    • GreatCorn
Re: Access Self from With statement
« Reply #5 on: July 28, 2020, 01:02:15 pm »
Turns out I was just stupidly doing everything in the wrong order. I had to Application.CreateForm first and then assign the TForm properties (with component as TForm do). I put Application.Initialize; and Application.CreateForm(TForm, component); first and then did the assigning and everything worked. I didn't even need to typecast component to TForm. Thanks everyone for your time

MarkMLl

  • Hero Member
  • *****
  • Posts: 8572
Re: Access Self from With statement
« Reply #6 on: July 28, 2020, 02:37:40 pm »
My apologies for focussing more on the language construct than on what turned out to be a problem with your use of the LCL.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018