天天看點

Delphi 10.3 變量初始化的時候同時指派

unit UnitloopInfo;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Data.DB, Vcl.StdCtrls,
  Vcl.Grids, Vcl.DBGrids;

type
  TFormloopInfo = class(TForm)
    Panel1: TPanel;
    DBGrid1: TDBGrid;
    Label1: TLabel;
    ComboBox1: TComboBox;
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormloopInfo: TFormloopInfo;
  deviceId:integer = -1;//在這裡初始化并指派

implementation

{$R *.dfm}

uses UnitMain,UnitDataModule;
           

注意:在聲明時初始化的方法隻适用于全局變量,這點在Delphi的代碼标準文檔裡可以找到。

[4). 變量

(1). 局部變量

局部變量用于過程内部,果需要的話,應當在過程的入口處立即初始化變量。局部的AnsiString 類型的變量自動被初始化為空字元串,局部的接口和dispinterface類型的變量自動被初始化為nil,局部的Variant和 OleVariant類型的變量自動被初始化為Unassigned。

(2). 全局變量

一般不鼓勵使用全局變量。不過,有時候需要用到。即使如此,也應當把全局變量限制在需要的環境中。例如,一個全局變量可能隻在單元的實作部分是全局的。

全局資料如果将由許多單元使用,就應移動到一個公用單元裡被所有對象使用。全局資料可在聲明時直接初始化為一個值。注意,所有全局變量自動進行零初始化,是以,不要将全局變量初始化為諸如0 、nil、或Unassigned等空值。零初始化的全局變量在.EXE檔案中不占空間。零初始化的資料儲存在虛拟的資料段中,而虛拟資料段隻在應用程式啟動時才配置設定記憶體。非零初始化的全局資料則在.EXE檔案中占空間。

繼續閱讀