在Delphi中声明公共全局变量 [英] Declare public global variable in Delphi

查看:148
本文介绍了在Delphi中声明公共全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在delphi项目中有两种形式,我想要能够从form2访问form1的变量。有没有人可以声明,在form1中可以看到一个'public'变量,这些变量可以从所有窗体中读取?



我已经尝试在公共声明中放置一个变量>

  {私有声明} 
public
{public declarations}
test:integer;
结束

在表单2中我有



<$ p单位Unit2; $ p>

{$ mode objfpc} {$ H +}

界面

使用
类,SysUtils,FileUtil,窗体,控件,图形,Dialogs,unit1;

type

{TForm2}

TForm2 = class(TForm)
procedure FormClose(Sender:TObject; var CloseAction:TCloseAction) ;
procedure FormCreate(Sender:TObject);
private
{私人声明}
public
{public declarations}
end;

var
Form2:TForm2;
实现

{$ R * .lfm}

{TForm2}

程序TForm2.FormCreate(发件人:TObject);
begin
form1 // <---------- Do not NOT RECOGNIZED
end;

结束。

然后我将Unit1放入Form2的使用部分,但似乎我不能由于循环引用。如果可能,我想避免使用指针。

解决方案

首先,如果您必须使用全局变量(Craig明智地指出,最好不要使用全局变量),那么你应该把你想在SharedGlobals.pas中共享的全局变量:

  unit SharedGlobals; 
接口
var
{variables here}
Something:Integer;
实现
{这里没有?}

现在使用该单元您想要共享两个单位的访问权限。或者,同时引用另一个对象,它被命名为一些明智的对象,并将该对象设计为状态(变量值)的持有者,这两个实例(表单或类) ,或任何东西)需要分享。



第二个想法,由于您的两个单元您已经有依赖关系,您也可以通过使用创建循环依赖关系的单元,从实现部分而不是接口来绕过循环依赖关系:

 单位单元2; 
接口
///东西
实现
使用Unit1;

...

单位Unit1; 
接口
///东西
实现
使用Unit2;


Let's say I have two forms in a delphi project, I want to be able to access form1's variables from form2. Is there anyone to declare, say a 'public' variable in form1 which can be read from all forms?

I have tried putting a variable in the public declaration

    { private declarations }
  public
    { public declarations }
  test: integer;
  end;     

and in form 2 i have

    unit Unit2;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, unit1;

type

  { TForm2 }

  TForm2 = class(TForm)
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end; 

var
  Form2: TForm2; 
implementation

{$R *.lfm}

{ TForm2 }

procedure TForm2.FormCreate(Sender: TObject);
begin
  form1 //<---------- DOES NOT GET RECOGNIZED
end;

end.

I then put 'Unit1' into the uses section on Form2, but it seems I can't do that due to a circular reference. I'd like to refrain from using pointers if possible.

解决方案

First, if you must use globals (it's probably better not to use globals, as Craig has wisely pointed out) then you should put the globals you want to share in SharedGlobals.pas:

unit SharedGlobals;
interface
var
   {variables here}
   Something:Integer;
implementation
    { nothing here?}

Now use that unit, from the two units you want to share access to that variable in. Alternatively, have both reference another object, which is named something sensible, and have that object be designed, as the holder of state (variable values) that those two instances (forms or classes, or whatever) need to share.

Second idea, since your two units that you have already have dependencies on each other, you could also get around your circular dependency by using the unit that would create a circular dependency, from the implementation section instead of the interface:

 unit Unit2;
 interface
   /// stuff
 implementation
    uses Unit1; 

...

 unit Unit1;
 interface
   /// stuff
 implementation
    uses Unit2; 

这篇关于在Delphi中声明公共全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆