Use of global variables and class variables in Delphi

Asked

Viewed 7,569 times

8

I have some questions regarding the use of global variables and class variables using class var at Delphi.

Declaring class variables with class var.

unit Unit1;

interface

type
  TClass = class
  public
    class var _Object: TObjectList<Integer>
  end;

implementation
end.

Declaring global variables

unit Unit1;

interface

var
  _Object: TObjectList<Integer>

implementation
end.

Question:

How does the compiler allocate memory for these statements? Which form of use is the most suitable to be used?

  • 2

    Depends on the intention. If it is a support variable for the class or all instances of that class (or subclasses), it must be a class var. If you have interest outside the class and that interest is shared and unique, maybe you should be a var global, although prefer whenever possible to use a field of a Singleton object, to group fields/properties logically. And that object is in a global variable or is accessible/instantaneous through another global object.

  • In general I think it is better to minimize the use of global variables and tend to put the other variables in the classes.

6 answers

6

  • You can mark your own answer as accepted.

4

After the separation of Codegear from Borland, the team responsible for Delphi sought to modernize the language. In this sense, he added language constructs that aim to allow better programming practices, notably by giving the application a more object-oriented model.

Public and global class variables are relatively similar, in the sense that they will have only one instance, but class variables have the advantage of being defined within the scope of the class, which avoids possible collisions of names in different units or even within it unit.

For the latter reason I recommend using class variables.

3

In any development environment that provides scope facilities for variables so you can create members in the Class (Java, Delphi, C#, etc.) always use these features instead of global or class variables as these variables will create excessive coupling in your code making maintenance difficult. The Design Pattern Singleton allows you to create something better than simply using Global Variables but should also be avoided.

1

Just to add to what has already been said.

I’ve seen two global variables in different Unit s but with the same name. They were the type of a class. When the unit1 was instanced, the unit2 was instantiated. When destroying, the unit1 was destroyed. Result: "Access Violation".

As it was virtually impossible to change the staff culture relative to var Global, I instructed them to declare within the scope of implementation in Unit, rather than the interface. This made it not visible to other Units, temporarily solving the problem.

1

The programming language behind Delphi is Object Pascal whose origin is in structured Pascal, that is, not object oriented. For this reason it exists in the global variable support syntax. But in an Object Oriented program it is not a good practice to use global variables.

If you still want to use global variables keep in mind the following rules:

1- Global variables are not related to any class. 2- They are declared in the Unit INTERFACE section and when used it is good practice to follow the syntax Unitname.VariavelNome. Example: Unitpessoa.Qtdepopulacao := 1234; 3- They can be referred to in all program codes, Procedure, Function and methods that use Unit in which the variable is defined; 4- They can be initialized or finalized in the Unit INITIALIZATION AND FINALIZATION sections in which it is declared.

Static variables of the "Class Var" classes follow the following rules:

1- They are declared in the class definition and their value is shared by all instances of the class. 2- Class methods may refer to them;

My final suggestion:

1- Use global variable when it stores information related to the Unit in which it is declared, and not specific to a particular class of this Union; 2- In other cases, use class variable that is more elegant, more elucidative, and follows what is defined as good OO practice.

0

When you declare a variable in VAR in Unit this variable is static and is visible anywhere importing the Unit in question.

Summarizing: If unit1 has a variable varDaUnit1 and unit2 uses unit1, unit2 can access varDaUnit1 as if it were part of unit2. Any change in its value will be reflected to other Units using unit1.

When declaring a variable in public of the class it is a dynamic variable, which is created and destroyed along with the class. Access to variables in the public is via class, for example:

classe1.varDaClasseDecalradaNoPublic;

When working with Delphi the company where I worked outlined a set of rules regarding the use of var, discouraging its use. The only situation where I see that the use of var is plausible would be if I need a global variable shared across multiple screens of the system, e.g.: Logged-in user.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.