Delphi Variable Scope

Source: Internet
Author: User

Completely from the network (one piece of snow), for reference:

Many beginners to the Delphi unit variable and function access permissions do not understand, I would like to illustrate, I hope that beginners to read this article can be enlightened.

The variable and function access permissions for the Delphi unit are as follows two unit descriptions:

Unit Unit1;

Interface

Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, Stdctrls;

Type
=============================== object and its member area ===============================
TForm1 = Class (Tform)
Button1:tbutton;
Procedure Formcreate (Sender:tobject);
Procedure Button1Click (Sender:tobject);
Private
{Private declarations}
str1:string; Private variables and private functions defined in this area can only be used in the Unit1 of this unit
Public
str2:string; Public variables defined in this area can be used in other units, but must first refer to this cell uses unit1; then use UNIT1.FORM1.STR2; The member variable STR2 is a member of the object Form1 and must be Form1 by the object to get the member str2
function Public1 (a:string): String; Public functions defined in this area can be used in other units, but must first refer to this cell uses unit1; Then use UNIT1.FORM1.PUB1 (2); The member function pub1 is a member of the object Form1 and must be Form1 by the object to get the member function pub1
{Public declarations}
End
=============================== object and its member area ===============================

===================================== Global Zone =================================
The variables and functions defined in this area are global and visible to other cells, as long as the cell uses unit1 can directly refer to the variables and functions of that area.

Global variables are best written uniformly into a file.

TCHAR3 = array[0..2] of Char;
TString3 = array[0..2] of String;
function All (str:string): String; Global function, not part of an object
Var
Form1:tform1;
CHR:TCHAR3; Global variable, not part of an object
Str3:tstring3;
===================================== Global Zone =================================
Implementation

Uses Unit2;

{$R *.DFM}
==================================== Local Area ==================================
The variables and functions defined in this area are local and can only be used in this unit Unit1, which is not visible to the other units.
Var
str4:string;
function Local1 (a:string): String;
Begin
Result: = A;
End
==================================== Local Area ==================================

function Tform1.public1 (a:string): String;
Begin
Result: = A;
End

function All (str:string): String;
Procedure Localfunction; The local function defined here Dudu can only be used in function all (str:string): String;
Begin
ShowMessage (' function internal use ');
End
Begin
RESULT:=STR;
Localfunction;
End

Procedure Tform1.formcreate (Sender:tobject);
Begin
str1:= ' 1112 ';
str2:= ' 3333 ';
str4:= ' 4433 ';
End

Procedure Tform1.button1click (Sender:tobject);
Begin
Form2. ShowModal;
End

End.

Unit Unit2;

Interface

Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, Stdctrls;

Type
TForm2 = Class (Tform)
Button1:tbutton;
Procedure Button1Click (Sender:tobject);
Private
{Private declarations}
Public
{Public declarations}
End

Var
Form2:tform2;

Implementation
Uses Unit1;

{$R *.DFM}

Procedure Tform2.button1click (Sender:tobject);
Begin
Chr[0]:= ' C ';
ShowMessage (' global variable ' +chr[0]);
str3[0]:= ' Global variables ';
ShowMessage (Str3[0]);
ShowMessage (' public variable ' +unit1.form1.str2);
ShowMessage (Unit1.Form1.public1 (' public function '));
ShowMessage (All (' global function '));
ShowMessage (' private variable ' +UNIT1.FORM1.STR4); Can not reference
ShowMessage (' local function ' +unit1.form1.local1 (' DD ')); Can not reference
End

End.

Unit File Structure Example:
Unit Unt1;
Interface
Uses Windows, Messages, sysutils;
Type
Tfrm1 = Class (Tform)
Private
{Code1}
Public
{Code2}
End
Var
{CODE3}
Implementation
Uses untpublic;
{CODE4}
End

Scope:
1. Global variables declared at CODE3 can be accessed by Unt1 and other unit files belonging to the project (Project1) (as long as the other unit declares uses unt1)
2. The global variable declared at Code2 is the same as the variable scope at CODE3
3. Global variables declared at Code1 can only be accessed by Unt1 internal access even if other unit files are declared uses Unt1
4. Local variables declared at CODE4 can only be accessed within the function or method to which they belong
Life cycle:
The global variables declared at 1.CODE3 are destroyed at the end of Unt1 (as distinct from the end of the Tfrm1), most of which occurs at the end of the owning project (Project1)
Global variables at 2.code1 and Code2 are destroyed at the end of the Tfrm1 (such as Tfrm1.free)
Local variables at 4.CODE4 are destroyed at the end of the owning method or function call
Other than that
In the case where two uses statements are declared, the actual effect is the same, there is no difference.

The above is a personal understanding, there are errors or not all of them also hope to point out the correction.

Delphi Variable Scope

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.