如何在DELPHI工程中定义全局变量?

解决方案 »

  1.   

    我的方法是建一个BaseMethod.pas单元,在里面Interface部分定义变量,然后让工程所有的其它单元都引用这个单元就好了...那么你的变量就成了公共变量了...不知道其他的大牛有没有更好的方法...
      

  2.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      str: string;
      
    implementation{$R *.dfm}
    var
      str2: string;procedure TForm1.Button1Click(Sender: TObject);
    beginend;end.
    {里面的str,和str2都是全局变量.str2只能在本单元全局访问,str既能在本单元全局访问,也能
    在其他单元访问,其他单元通过Unit1.str访问}