怎么Const定义变量 ?我还是第一次听说
解决方案 »
- 我想更好更快地学习delphi7,请兄弟们支招。
- 自适应列宽问题,为什么这个处理过程不能完全实现效果呢?
- 请教:nvarchar型的数据模糊查询查不出来?
- delphi 如何调用c#写的dll?能不能给个demo阿?我找了好多地方,都没有找到阿!谢谢各位了!
- sql语句
- 50分奖励提供优秀报表控件及安装使用方法的大虾!
- 请问Delphi中如何进行多线程的编程呀,如何同步?
- 谁知道QQ中那种窗口滑动的效果是怎么做的?
- 用SQL进行统计个数时出错了,请各位高手帮助!
- Delphi的quickreport为何不能在NT上运行?
- 请教各位一个简单的问题,这个SQL语句如何写?
- 若有10个LABEL控件,label1,label2......label10,想通过FOR 语句一次完成;
i:integer=0;//i是不能变的,只能等于0,在pascal中就是这样,难道delphi1.0--delphi5.x行?
i: integer = 0; //而且i得是全局的才行。 //既然是变量,怎么用个const呢?
Form1: TForm1;const
i:integer=12;procedure TForm1.Button2Click(Sender: TObject);
begin
ShowMessage(IntTostr(i-1));
end;呵呵,奇怪的很,试试
i:integer=0;//i是不能变的,只能等于0,在pascal中就是这样,难道delphi1.0--delphi5.x行?
var//定义变量
i:integer=0;//i可被赋值,在pascal中就是这样,难道delphi1.0--delphi5.x不是这样?
const
i:integer=0;......i := 2;//可以吗?????
procedure TForm1.Button2Click(Sender: TObject);
begin
i:=10;
ShowMessage(IntTostr(i-1));
end;
var
i :Integer = 0;
是一样的。D6里可能作了限制,呵呵,我也觉得D5这样处理很不怎么好。唔,我去看看What's New.
我试试。
To javis(蓝枫):
知道你就说啊?D5下我当然知道能用了,我经常用的。你说说D6下为什么不能用吧?
to blucecat(阿水爱cat):
学习一下吧?(呵呵,开玩笑)。
OK了。 刚看了看帮助。
{$WRITEABLECONST ON}和{$J+}是等价的!!加上就好了。
D6缺省是{$J-},D5可能缺省是{$J+} 。所以才会出现这个问题。呵呵。问题解决!!
我刚刚试了一下,如果是全局变量,
var
i :Integer = 0;
没错。是局部变量的话,就出错了。(D6下)
就知道有人要问,试试这个,在D5中
procedure TForm1.Button2Click(Sender: TObject);
begin
i:=10;
ShowMessage(IntTostr(i-1));//这个i是var或const根本没关系,因为这句根本没对i赋值,当然正确了,
end;
var
Form1: TForm1;const
i:integer=0;procedure TForm1.Button1Click(Sender: TObject);
begin
i:= 10 ; //D6下,在这一句出错 。D5下没错。
ShowMessage(IntTostr(i));
end;
改成:
----------------------------------------------------
var
Form1: TForm1;var
i:integer=0;procedure TForm1.Button1Click(Sender: TObject);
begin
i := 10; //no problem
ShowMessage(IntTostr(i));
end;
或者:
-----------------------------
var
Form1: TForm1;
{$J+}
const
i:integer=0;procedure TForm1.Button1Click(Sender: TObject);
begin
i := 10; //no problem
ShowMessage(IntTostr(i));
end;=====================================
各位大虾继续讨论吧。:)
这是一个关于扑获power键的程序,如下:(声明:来自coDelphi.com)“//”后的话是我写的unit CPower;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;type
TForm1 = class(TForm)
procedure FormPaint(Sender: TObject);
private
{ Private declarations }
procedure WMPowerBroadcast(var message: TMessage); message WM_POWERBROADCAST;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}{ TForm1 }procedure TForm1.WMPowerBroadcast(var message: TMessage);
{$J+} //我加了这句后,D6下就对了!
const SkipNextPowerMsg:boolean=True; //这句在D6下出错!
//另外,请大家分析一下,上面的这句定义是不是类似C++中的Static 局部变量??
begin
if SkipNextPowerMsg then
begin
SetForegroundWindow(Self.Handle);
if Application.MessageBox('是否关闭系统?','警告',MB_OKCANCEL + MB_DEFBUTTON2)<>IDOK then
begin
message.Result := BROADCAST_QUERY_DENY;
SkipNextPowerMsg:=not SkipNextPowerMsg;
end
else
Close;
end
else
SkipNextPowerMsg:=not SkipNextPowerMsg;
end;procedure TForm1.FormPaint(Sender: TObject);
begin
Self.Visible:=False;
end;end.
const
s=ddddddddddddddd....
var
i:integer=0;
you try
基本的定义要搞清楚!
你还是把上面的例子趋试试吧?试试就知道了。:)
加了{$J+}常量可以当变量,不加只能是只读In the default {$J+} compiler state, typed constants can have new values assigned to them; they behave essentially like initialized variables. But if the {$J-} compiler directive is in effect, typed constants cannot change value at runtime; they are, in effect, read-only variables.
我是根据 {$WRITEABLECONST ON} 才找到 {$J+}的。
----------------------
"Rannce" <[email protected]> wrote:
> {$J+} compiler state is default in D5, and it's changed to {$J-}
> is the default state in D6?Correct. The default has changed.--
Dave Nottage
-----------------------------------------------
:)
const是定义常量的关键字,但本人从学delphi开始(始于delphi3)就知道:
形如“const i:integer=1;”的格式并不表示i为常量,而是叫做“类型常量”,是一种新的数据结构,可以看作是初始化了的变量。
系统只会对该类型常量初始化一次,而不论该类型常量被调用多少次!!
Syntax {$J+} or {$J-}
{$WRITEABLECONST ON} or {$WRITEABLECONST OFF}
Default {$J+}
{$WRITEABLECONST ON}
Scope Local
The $J directive controls whether typed constants can be modified or not. In the {$J+} state, typed constants can be modified, and are in essence initialized variables. In the {$J-} state, typed constants are truly constant, and any attempt to modify a typed constant causes the compiler to report an error.
In previous versions of Delphi and Borland Pascal, typed constants were always writeable, corresponding to the {$J+} state. Old source code that uses writeable typed constants must be compiled in the {$J+} state, but for new applications it is recommended that you use initialized variables and compile your code in the {$J-} state.我稍译一下吧,译得不好不许骂我:
<--开始-->
$J伪指令控制类型常量能否被修改。设定{$J+}时,类型常量能被修改,其本质上是一个初始化了的变量。设定{$J-}时,类型常量是真正的常量,如果试图在程序中修改它会引起编译错误。
在以前版本的Delphi和Borland Pascal中,类型常量总是可写的,相当于{$J+}状态。使用可写类型常量的旧版源程序必须在{$J+}状态下编译,但是推荐新应用程序使用初始化的变量并在{$J-}状态下编译。
<--结束-->我注意到D5默认状态为{$J+},D6还没用,不知道,应该如kylix2001所说,变成了{$J-}
initialized variables应该指的是:
var
i: Integer = 1;
所以在D6中,默认是{$J-}。详见:http://www.csdn.net/develop/article/8/8808.shtm