本人有个问题:
 1、如何在delphi中定义一个全局量,使每个窗体都可以用到它。

解决方案 »

  1.   

    尽量不使用全局变量,如必须使用全局变量则必须加前缀‘g’,同时应在变量名称中体现变量的类型。例如:
      gprecUserCount: point;//名称为UserCount的全局变量,其类型为指向一结构的指针
    但是在模块内部可以使用全局变量。所有模块内全局变量必须用‘F’为前缀。如果几个模块之间需要进行资料交换,则需要通过声明属性的方法来实现。例如:
    type
      TFormOverdraftReturn = class(TForm)
      private
    { Private declarations }
    FuserName: string;
    FuserCount: Integer;
    Procedure SetUserName(Value: string);
    Function GetUserName: string;
      public
    { Public declarations }
    property UserName: string read GetUserName write SetUserName;
    property UserCount: Integer read FuserCount write FuserCount;
      end;
      

  2.   

    1.在工程项目中添加一个单元文件,让每个窗体单元都引用单元文件
    unit Common
     
    interface
    uses
     //引用的单元var
     //将全局变量在这里声明
    在窗体单元中
    Unit FormA
    interface
     ....,common;
    2.可以将全局变量声明在一个窗体单元文件中
    unit ViewNews;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      ........type
      TViewForm = class(TForm)
      private
        { Public declarations }
      public
        { Public declarations }  end;var
      ViewForm: TViewForm;
      //在这里作全局变量声明
    在窗体单元中
    Unit FormA
    interface
     ....,ViewNews;