I have a console application written in Delphi. I saw that I can have global variables by assigning them to unit scopes, but in a console application I don’t use units (from what I’ve understood it’s forms-only).
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Nope, a unit is not equivalent to a form.
A unit is a module which contains part of your program. Each form is a separate unit but a unit does not have to contain a form.
Each unit has an interface section and a implementation section. The declarations in the interface section are visible to all units that use the unit:
You can declare global variables by declaring them in a unit:
GVar1 is visible and can be modified by all units using unit A. GVar2 is only visisble by the code of unit A because it is defined in the implementation section.
I strongly advice against using globals in the interface section because you have no control over them (because anybody can change them). If you really need a global, you better define it in the implementations section and provide access functions.
By the way, you can see a unit as a kind of a class (with a single instance). It even has a way to construct and destruct: