Unit unit1;
Interface
Uses
Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs
Type
TFORM1 = Class (TFORM)
Button1: tbutton;
Procedure Button1Click (Sender: TOBJECT);
Private
{Private Declarations}
public
INT_A: INT; // *** 1
{Public declarations}
END;
VAR
FORM1: TFORM1;
INT_B: INT; // *** 2
IMPLEMENTATION
{$ R * .dfm}
VAR
INT_C: INT; // *** 3
Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);
Begin
// do something
END;
End.
Global variables must have a lot of use, but it can be defined in multiple places (perhaps one or several places definition is wrong), I am the most basic unit of the above, in the star 1 I can define global variables in 2, 3 places. I think that the three are distinguished, the difference is that 1, 2 is defined, and the other Unit can be used, and 3 defined only in this unit. What is the difference between 1,2? Or what else else is there? I also hope that everyone will discuss it!
-------------------------------------------------- -------------
Location 1 Definition: It is a variable of TFORM1, if there are other classes or other units in this unit, then "FORM1." Will be written before referenced.
Position 2 Definition: It is the variable of this unit, so all classes or other units in this unit (of course, to reference this unit) can be directly referenced.
The above is my own fool, please let me know!
-------------------------------------------------- -------------
1 Defined global variables have your form object to maintain, and 2 define no one to maintain it (unit file maintenance), defined to 1 place is good, at least meet the concept of objects! I don't like to use global variables, I will replace global variables with global variables, although creating an object, and creating a variable is different, and efficiency is different, but it is easy to change the program for easy maintenance.
-------------------------------------------------- -------------
It is recommended to see that Delphi helps "Object Pascal Reference" help the topics for scope (Scope), will have a great help. I posted a small paragraph:
Naming conflict
When a block encapsulates another block, the former is called an external block, and the latter is called an internal block. If the identifier in the external block is re-declared in the internal block, then in the entire internal block, the identifier of the internal block declaration will ignore the declaration in the external block (that is, the compiler is in the entire interior This identifier is interpreted and used in the block as a declaration of internal blocks). For example, if the variable MaxValue is declared in the interface section of the unit, then declare the same name variable in a function in the unit, and the reference to maxValue in the function block is not crossed in a function block. Explain that local variables, that is, the declaration in the function. Similarly, if another function is created, another function is used in the external scope, and the partial variables of the same name can also be re-declared in the internal domain (see the nesting definition of the process and function in the current topic editor note). Multiple unit references make the definition of the scope more complicated. Each unit column in the USES clause enforces a new scope that encapsulates other units, programs, and units included in the unit USES clause. The first unit in the USES clause represents the outermost functional domain, and the following units represent a new scope within the scope. If the same identifier is declared in the interface feature of two or more units, the declaration in the innermost scope is selected when referenced by the identifier without crowned to limit the word, that is, the selection reference The unit of the identifier, or, if there is no declaration in the unit of the reference this identifier, the last declares that declares the identifier in the unit list of the unit USES clause. (See the influence of the USES clause to the role domain in the interface festival in the current topic Editor's Note and the impact of the USES clause to the role domain in the interface festival)
The System unit provided by Borland is automatically used by each program or unit. All statements in the System cell and the compiler can automatically identify predefined types, routines, constant, etc., always as the outermost scope. (See the only special SYSTEM unit in the current topic editor's note)
The declarations of the inner layer scope can be around the inner layer scope by using the identifier (see a restriction identifier) or the WITH statement (see statement).