3 9 Votes

Delphi: Define global variables

Tip by Delphian | 11/02/2013 at 10:51

In this tip, I show you, how to define global variables in Delphi, so that these variables can be accessed 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.

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

Avatar AuthorThe author has not added a short description to his profile yet.
Show Profile | Message

 

Related Topics

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 articles on askingbox.com. How to do.