全局变量怎马定义?各位帮帮忙,在线等,捉摸了三天啦......................

sh_yangxiangming 2008-01-18 08:11:34
1、有人说在这里,可是不行:

unit Unit2;

interface
wlsjk: string ;
bdsjk: string ;
userid:string;
2、有人说在这里,还是不行;
unit Unit2;
interface
uses
SysUtils, Classes;
type
TDataModule2 = class(TDataModule)
private
{ Private declarations }
public
wlsjk: string ;//这三个变量
bdsjk: string ;
userid:string;
{ Public declarations }
end;
var
DataModule2: TDataModule2;
implementation
{$R *.dfm}
end.
3、在这里,勉强不提失错,但调用它的unit里,现实是''值,我在前面的unit里明明已经赋值。
unit Unit2;
interface
uses
SysUtils, Classes;
type
TDataModule2 = class(TDataModule)
private
{ Private declarations }
public
wlsjk: string ;//这三个变量
bdsjk: string ;
userid:string;
{ Public declarations }
end;
var
DataModule2: TDataModule2;
implementation
{$R *.dfm}
end.

求求大家拉,我不想再捉摸三天。
...全文
106 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
sa036 2008-05-11
  • 打赏
  • 举报
回复
你在UNIT2里用VAR定义这三个变量,在USES UNIT1
unit UNIT2;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, Db, DBTables;

type
...
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;
WLSJK:string;
implementation
uses UNIT1,UNIT3;
{$R *.DFM}
...

然后在UNIT1里:uses UNIT2,UNIT3;
在你想赋值的地方赋值: wlsjk='我是菜鸟' ;
在UNIT3里:uses UNIT1,UNIT2;
zhuchengchuan 2008-05-10
  • 打赏
  • 举报
回复
unit Unit1
type
TForm1 = class(TForm)
private
变量A :string;

{ Private declarations }
public
变量B :string;
{ Public declarations }
end;

var
变量C :string;
form1:TForm1;
....
其他单元引用此单元,但是访问变量时有区别;

变量A只能是在Form1内容访问,其他单元不能访问;
变量B的访问方式:Form1.变量B := 'public类型的变量访问时要加窗体名称';
变量C的访问该项:变量A := '全局变量直接访问';

这样应该可以,不知道其他人有什么更好的方法?
jzx19770812 2008-01-25
  • 打赏
  • 举报
回复
定义在实现声明上面,然后别的单元use一下就可以了,
不过从面向对象的角度说,全局变量还是少用
bridge05 2008-01-24
  • 打赏
  • 举报
回复
eg:

unit: global.pas
------------------------
unit global;

interface


implementation

var
g_Name: string;

end.


unit: employe.pas
------------------------
unit employe

interface

implementation

uses
global; //引用变量单无


produce setName(value: string);
begin
global.g_Name := Value; //使用变量

end;
御影北斗 2008-01-24
  • 打赏
  • 举报
回复
在implementation前面定义
阿三 2008-01-18
  • 打赏
  • 举报
回复
不至于这么难吧,给你写一个
unit1单元的内容:建立窗体1
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button2: TButton;
Button3: TButton;
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Unit2, Unit3;
{$R *.dfm}
procedure TForm1.Button2Click(Sender: TObject);
begin
wlsjk := trim(Edit1.Text);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
if not assigned(form3) then
form3 := TForm3.create(Application);
form3.ShowModal;
form3.free;
form3 := nil;
end;
end.

unit2单元内容:只建一个单元文件,定义变量
unit Unit2;
interface
var
wlsjk: String;
implementation
end.

unit3单元内容(建一个窗体,放一个按钮,引用unit2)
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm3 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form3: TForm3;
implementation
uses Unit2;
{$R *.dfm}
procedure TForm3.Button1Click(Sender: TObject);
begin
showmessage(wlsjk);
end;
end.
sh_yangxiangming 2008-01-18
  • 打赏
  • 举报
回复
举个例子:
我在unit2定义了上面的三个变量;
我在unit1里给他赋值wlsjk='我是菜鸟'
可当运行到Unit3时,这个wlsjk='',不是'我是菜鸟'
就这个意思,为什吗?
sh_yangxiangming 2008-01-18
  • 打赏
  • 举报
回复
不好意思,我是菜鸟,我已经按上面的做啦,可是引用了那个单元,还是不行,那个变量时空的。前面赋的值,不能带到其他单元
阿三 2008-01-18
  • 打赏
  • 举报
回复
怎么定义全局变量?
按你的问题,你可以建立一个公共单元,把全局变量都定义在这个unit中,其它的都引用这个单元就可以了
byteh 2008-01-18
  • 打赏
  • 举报
回复
不明白你要干啥,这不是什么问题呀?
sh_yangxiangming 2008-01-18
  • 打赏
  • 举报
回复
对,补充一下,我在其它unit里已经引用了这个unit2.

2,496

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧