如何在delphi中定义全局变量?(50分)

A

abea

Unregistered / Unconfirmed
GUEST, unregistred user!
如何在delphi中定义全局变量。比如:我要记录登录者的信息,该信息我在全局要用,
可是我该如何定义才能成为全局变量了?
 
faint.
----------------------------
var //Global variable.
MainForm: TMainForm;
FinishEvent: Thandle

TemList, FileNameList: TStringList;
CurFileName: string;

implementation
 
没有全局变量的说法,定义在interface中,然后引用这个单元
 

unit1;
var
Form1:Tform1;
a:String;//这就是了
implementation
.......
要用到a的话,uses unit1就可以了
 
要不用Singleton模式也可以
 
最好是放在一个没有表单的公共单元里(菜单file-new-unit)。包括公共函数、过程也都
可以在这里定义。要使用这些变量或函数的单元只需要uses一下这个单元即可。例子如下:
unit PubUnit;

interface

uses Windwos,Messages,SysUtils,Registry.......;

var G_AppDirect:string
//公共变量
G_integer:integer;

function iif(B:boolean,Tj1,Tj2:Variant):Variant
//公共函数

implementation

function iif(B:boolean;Tj1,Tj2:Variant):Variant;
begin
if B=true then
Result:=Tj1
else Result:=Tj2;
end;

end.
 
同意hunterteam的說法.
 
我需要的是跨越多个单元的全局变量。
我定义了一个类TUser,要使Tuser的对象跨越多个单元。
在一个单元中,得到用户的信息,在其他单元中用。
请给个办法。
 
前面几位已经说得很明白了,这么简单,你为什么不试一下呢?
 
前面的大家都说的太清楚了
自己好好试一下就知道了
 
前面的方法我早就都试过了,不行阿。
我还在 工程文件中定义了个变量,可惜,一到其他的单元中,就不认该变量。
你试一试好了。
 
faint!!!

uses user.pas
 
thank you a lots.
 
顶部