新手的极菜之问题~

nchen123 2003-09-02 01:21:31
我昨天刚开始学 delphi, 正在看 delphi.about.com 上的在线教程。
学到了静态变量,为什么这段简单的示例代码都编译不过:

procedure TForm1.Button3Click(Sender: TObject);
const clicks : Integer = 1; // 静态变量
begin
Form1.Caption := '您点击了' + IntToStr(clicks) + '次';
clicks := clicks + 1; ------->这一行出错!
end;

出错信息是
[Error] Unit1.pas(82): Left side cannot be assigned to

我用的是 delphi 6.0 的英文版。
...全文
48 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
nchen123 2003-09-02
  • 打赏
  • 举报
回复
不是我想用,是我刚刚学到这里,总得知道怎么用吧,

揭帖了
jacky_shen 2003-09-02
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
{$J+}
const
I: Integer = 0;
{$J-}
begin
Inc(I);
Caption := IntToStr(I)
end;

也可以通过设置编译指令J,来达到局部有效的作用。
sailer_shi 2003-09-02
  • 打赏
  • 举报
回复
你为什么要用静态变量呀?
nchen123 2003-09-02
  • 打赏
  • 举报
回复
sorry, 我没往下看,现在找到原因了:

要把 Project -> Options -> Complier -> Assignable typed constants 选项打开。
zhudongpo 2003-09-02
  • 打赏
  • 举报
回复
up
nchen123 2003-09-02
  • 打赏
  • 举报
回复
是不是这里的教程讲错了?

Variable typed constants
Although we declare typed constants in the const section of a procedure, it is important to remember that they are not constants. At any point in your application, if you have access to the identifier for a typed constant you'll be able to modify its value.

To see typed constants at work, put a button on a blank form, and assign the following code to the OnClick event handler:

procedure TForm1.Button1Click(Sender: TObject);
const clicks : Integer = 1; //not a true constant
begin
Form1.Caption := IntToStr(clicks);
clicks := clicks + 1;
end;

Notice that every time you click on the button, forms caption increments steadily.
chinalian 2003-09-02
  • 打赏
  • 举报
回复
常量clicks的值不能被更改。
guanghui 2003-09-02
  • 打赏
  • 举报
回复
那是常量,常量是不能用来计算的,跟VB中不同

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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