Using global variables and class variables in Delphi

8

I have some doubts about using global variables and class variables using class var in 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 most suitable for use?

    
asked by anonymous 21.02.2014 / 18:18

6 answers

6

The memory allocation process of a class variable using class var is the same as a global variable.

The only difference is that the variable declared as class var will have a class scope.

link

    
13.03.2014 / 13:39
4

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

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

For the latter reason I recommend using class variables.

    
25.02.2014 / 18:15
3

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

    
25.02.2014 / 00:31
1

Just to increase what has already been spoken.

I've already seen two global variables in different unit's but with the same name. They were kind of a class. When instantiating it in unit1, it was instantiated to unit2. At the time of destroying, it was then destroyed that of unit1. Result: "Access Violation".

Since it was practically impossible to change the staff culture against the Global var, I instructed them to declare within the scope of implementation in the unit, rather than the interface. This made it not visible to the other units, temporarily resolving the issue.

    
14.03.2014 / 14:01
1

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

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

1- Global variables are not related to any class. 2- They are declared in the INTERFACE section of the unit and when used it is good practice to follow the syntax UnitName.VariableName. Example: UnitPessoa.QtdePopulacao: = 1234; 3- They can be referred to in all program codes, procedure, function and methods that use the unit in which the variable is defined; 4- They can be initialized or finalized in the INITIALIZATION AND FINALIZATION sections of the unit in which it is declared.

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

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

My final suggestion:

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

    
14.03.2014 / 17:08
0

When you declare a variable in the VAR in unit this variable is static and is visible in any place that imports the unit in question.

In short: 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 that use unit1.

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

classe1.varDaClasseDecalradaNoPublic;

When working with Delphi the company where I worked drew 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 you need a global variable shared across multiple system screens, eg User logged in.

    
25.02.2014 / 17:37