在Delphi 7中,为什么要为const赋值? [英] In Delphi 7, why can I assign a value to a const?

查看:122
本文介绍了在Delphi 7中,为什么要为const赋值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一些Delphi代码从一个项目复制到另一个项目,发现它不会在新项目中编译,尽管它在旧的项目中。代码看起来像这样:

 程序TForm1.CalculateGP(..)
const
价格:钱= 0;
begin
...
价格:= 1.0;
...
end;

所以在新项目中,Delphi抱怨说左侧不能分配 - 可以理解!但是这个代码在旧的项目中编译。所以我的问题是,为什么?是否有一个编译器开关来允许const被重新分配?这甚至如何工作?我认为在编译时const已经被它们的值替换了。

解决方案

你需要转换可分配的类型的常量。

项目 - >选项 - >编译器 - >可分配类型的常量



您还可以添加 {$ J +} {$ WRITEABLECONST ON} 到pas文件,这可能更好,因为即使将文件移动到另一个项目,它也会工作。


I copied some Delphi code from one project to another, and found that it doesn't compile in the new project, though it did in the old one. The code looks something like this:

procedure TForm1.CalculateGP(..)
const
   Price : money = 0;
begin
   ...
   Price := 1.0;
   ...
end;

So in the new project, Delphi complains that "left side cannot be assigned to" - understandable! But this code compiles in the old project. So my question is, why? Is there a compiler switch to allow consts to be reassigned? How does that even work? I thought consts were replaced by their values at compile time?

解决方案

You need to turn assignable typed constants on. Project -> Options -> Compiler -> Assignable typed Constants

Also you can add {$J+} or {$WRITEABLECONST ON} to the pas file, which is probably better, since it'll work even if you move the file to another project.

这篇关于在Delphi 7中,为什么要为const赋值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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