410 Votes

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.

ReplyPositiveNegative

About the Author

AvatarThe author has not added a profile short description yet.
Show Profile

 

Related Topics

Tips to drink more water

Tip | 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.