Delphi – Create a form that has not been declared – I don’t know why

I am using Delphi 7 (I know it is antique) and I am a little confused about the form I am creating and break it when I complete it.

< p>From my main form, I created another form requesting a username and password. The newly created form attributes, etc. are contained in another unit and contained in the Uses clause.

In My main form, I used to “have” the following code;

var
MyOtherForm: TMyotherform;
Begin
MyOtherForm := TMyotherform. create(Nil);

{Then I do stuff-blah blah }

MyOtherForm.free;
End;

My question Yes, when I remove the declaration of MyOtherForm in my main unit, it still works. For example;

{ var // removed
MyOtherForm: TMyotherform ; // removed }

Begin
MyOtherForm := TMyotherform.create(Nil);

{Then I do stuff }

MyOtherForm.free;
End;

The same result, the form is created and destroyed as usual. What I cannot understand is why. In the past, did I declare my form in the main unit, or in a Is it enough to declare it in a separate unit, am I doing it wrong?

By default, Delphi creates a global variable for the form. It is added below the class declaration of the form .

The name of the variable is the class name minus the’T’, so it is the same as the name used for your local variable, which is why the code is still valid: you just put the global variable A new reference is stored in the .

If you have an auto-created form, Delphi will create an instance of the form when you start the application, and store the reference in the global. You can manage the auto-creation in the project options Form and data module, or you only need to edit the dpr file, in the file you will find the following line:

Application.CreateForm(TMyotherform, Myotherform);

But even if your form is not created automatically, Delphi will still add the global variable.

Personally, I don’t like these global variables at all, I am always creating forms or data Manually delete them when modules. Unfortunately, it seems impossible to configure it.

So: delete the global and declare local variables, just like in the original code. This is the correct way. Globalization is for Make it easier for beginners, but it does not help the maintainability of your application.

I am using Delphi 7 (I know it is antique) and I am right The form I’m creating is a bit confused and breaks when I complete it.

From my main form, I created another form that requests a username and password. Newly created form properties Etc. are contained in another unit and contained in the Uses clause.

In my main form, I used to “have” the following code;

var
MyOtherForm: TMyotherform;
Begin
MyOtherForm := TMyotherform.create(Nil);

{Then I do stuff-blah blah }

MyOtherForm.free;
End;

My problem is that when I delete the declaration of MyOtherForm in my main unit, it still works. For example; p>

{ var // removed
MyOtherForm: TMyotherform; // removed }

Begin
MyOtherForm := TMyotherform.create(Nil);

{Then I do stuff }

MyOtherForm.free;
End;

The same result, the form is created and destroyed as usual. What I cannot understand is why. In the past, did I declare my Is the form, or is it sufficient to declare it in a separate unit, am I doing it wrong?

By default, Delphi creates a global variable for the form. It is added under the class declaration of the form.

The The name of the variable is the class name minus the’T’, so it is the same as the name used for your local variable, which is why the code is still valid: you just stored a new reference in that global variable.

< p>If you have an auto-created form, Delphi will create an instance of the form when you start the application and store the reference in that global. You can manage the auto-created form and data module in the project options, or you can just edit the dpr File, in the file you will find the following line:

Application.CreateForm(TMyotherform, Myotherform);

But even if your form is not created automatically Yes, Delphi will still add the global variables.

Personally, I don’t like these global variables at all, and I always delete them manually when creating forms or data modules. Unfortunately, it doesn’t seem to be It may be configured.

So: delete the global and declare local variables, just like in the original code. This is the right way. Globalization is to make it easier for beginners, but it doesn’t help you The maintainability of the application.

Leave a Comment

Your email address will not be published.