Recent

Author Topic: Can't access class in form  (Read 382 times)

nikel

  • Sr. Member
  • ****
  • Posts: 282
Can't access class in form
« on: May 01, 2026, 07:55:34 pm »
Hello, I'm trying to call my class in form create event. I declated a variable named D however can't access it. Here's my code so far:
Code: Pascal  [Select][+][-]
  1. var
  2.   D       : TDownload;
  3.  
  4. implementation
  5.  
  6. {$R *.lfm}
  7.  
  8. { TForm1 }
  9.  
  10. procedure TForm1.FormCreate(Sender: TObject);
  11. begin
  12.   ShowMessage(Assigned(D).ToString()); // Outputs 0
  13. end;

I create the class as:
Code: Pascal  [Select][+][-]
  1.   TDownload = class(TForm1)
  2.   private
  3.     LinkToFile    : string;
  4.     TaskToKill    : shortstring;
  5.   public
  6.     constructor Create(AOwner: TComponent); reintroduce;
  7.     constructor Create(AOwner: TComponent; L: string; T: shortstring); reintroduce;
  8.     destructor Destroy; override;

I'm calling the create event in main lpr file. How can I use classes and forms correctly?

ASerge

  • Hero Member
  • *****
  • Posts: 2497
Re: Can't access class in form
« Reply #1 on: May 01, 2026, 08:14:24 pm »
Where D assigned?

Nicole

  • Hero Member
  • *****
  • Posts: 1324
Re: Can't access class in form
« Reply #2 on: May 11, 2026, 04:05:15 pm »
What happens, if you put this not in the create event but in OnResize?
This event is just the first one I came across, which happens "later".

Handoko

  • Hero Member
  • *****
  • Posts: 5545
  • My goal: build my own game engine using Lazarus
Re: Can't access class in form
« Reply #3 on: May 11, 2026, 04:58:27 pm »
I'm calling the create event in main lpr file.

It might (???) work but because D is declared inside TForm1, it makes more sense calling D := TDownload(self) in TForm1's OnCreate event.

Paolo

  • Hero Member
  • *****
  • Posts: 717
Re: Can't access class in form
« Reply #4 on: May 11, 2026, 05:09:04 pm »
Out of curiosity, why are you using "reintroduce" ?

Nicole

  • Hero Member
  • *****
  • Posts: 1324
Re: Can't access class in form
« Reply #5 on: May 11, 2026, 05:43:27 pm »
@Handoko
I would give it a try. Just half a minute of work.

Some days ago I had a similar problem.
If the tests is positive, I explain it in detail. If not, it would be a waste.

jamie

  • Hero Member
  • *****
  • Posts: 7765
Re: Can't access class in form
« Reply #6 on: May 11, 2026, 11:01:23 pm »
You need to use "Override" not "Reintroduced"

The first create should be ok with that but the section which does not have a Constructor of that type you simply use "Virtual" and use the
CTRL+Shift+C to auto generate the body code.

 In the Override version, you need to call the inherited Create(aowner) .... etc.

in the non, overridden on you still need to call the Create(....) constructor.

As for your Assigned returning 0 instead of a Boolean word, maybe you don't have the SysUtils in the uses list, those are helpers?

Jamie
 
The only true wisdom is knowing you know nothing

egsuh

  • Hero Member
  • *****
  • Posts: 1800
Re: Can't access class in form
« Reply #7 on: May 12, 2026, 06:44:12 am »
TForm1 is the anscestor class of TDownload.
D must be instantiated somewhere else.

 

TinyPortal © 2005-2018