Delphi: Define global variables
Tip by Delphian | Last update on 2023-01-29 | Created on 2013-02-11
In this tip, I show you, how to define global variables in Delphi, so that these variables can be accessed, set and used in each unit of your Delphi project you want.
A first idea might be to define the variable directly in the Delphi Project File (DPR). However, this is not easily possible, since it contradicts the hierarchy of Delphi.
For this reason, we simply define a new unit that can look something like this:
unit globalvars; interface var globalstr: string; globalint: integer; implementation end.
We named the unit globalvars, so it must also be saved as globalvars.pas. We have defined the two variables globalstr and globalint, but you can also declare any other variables such as arrays, objects and so like.
Now, we can integrate this unit into any other unit including our project file (DPR), by adding the unit to our USES list:
uses globalvars;
With this, the variables can be read and written globally in each unit. For example, we can set a global variable in our DPR file to a value and then query this value in any other unit. This can be used, for example, for global settings such as language settings of the program.
About the Author
The author has not added a profile short description yet.
Show Profile
Related Topics
How to Replace multiple Texts at the same Time
Tutorial | 0 Comments
XAMPP: How to set up SSL/HTTPS for local Projects
Tutorial | 0 Comments
Delphi: Global Treatment of Errors (Exceptions)
Tutorial | 0 Comments
Tips to drink more water
Tip | 0 Comments
Delphi: Multiline Caption for TLabel (at Run Time and Design Time)
Tutorial | 1 Comment
Website Performance: Caching and Expires Header for Images, CSS and JavaScript
Tutorial | 0 Comments
Delphi: System-Wide HotKey
Tutorial | 1 Comment
Important Note
Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.
Participate
Ask your own question or write your own article on askingbox.com. That’s how it’s done.