简体   繁体   English

为什么我的Delphi不接受私下声明中的任何内容?

[英]Why won't my Delphi accept anything under a private declaration?

I am a student and Delphi is not really my primary environment for programming. 我是一名学生,Delphi并不是我编程的主要环境。 I know how it works and I'm completely familiar with its layout. 我知道它是如何工作的,并且我完全熟悉它的布局。 I recently received a task in which I have to use arrays in order to list them in a listbox via integer and string. 我最近收到了一项任务,其中必须使用数组才能通过整数和字符串在列表框中列出它们。 The idea is very simple and the program itself is very easy to make. 这个想法很简单,程序本身也很容易制作。

The problem comes when I try to declare a constant under the private or public it just shows and error message stating "'END' expected but 'CONST' found". 当我尝试在私有或公共变量下声明一个常量时,就会出现问题,并显示错误消息,指出“预期为'END',但找到了'CONST'”。 I have worked with public and private variables and constants for a while now but I'm not really sure what's going on. 我已经使用公共和私有变量以及常量已有一段时间了,但是我不确定是怎么回事。

The code is as follows: 代码如下:

type
  TForm1 = class(TForm)
    edtDateTime: TEdit;
    lstListArrayValues: TListBox;
    gbpIntegerArrayOptions: TGroupBox;
    gbpStringArrayOptions: TGroupBox;
    gbpListBoxOptions: TGroupBox;
    btnInitializeIntArray: TButton;
    btnAssignIntArray: TButton;
    btnDoubleIntArray: TButton;
    btnInitializeStrArray: TButton;
    btnAssignStrArray: TButton;
    btnCapitalStrArray: TButton;
    btnDisplayArray: TButton;
    btnClearListbox: TButton;
    btnDeleteSelected: TButton;
    XPManifest1: TXPManifest;
    procedure FormCreate(Sender: TObject);
    procedure btnInitializeIntArrayClick(Sender: TObject);

    private
    {Private Declarations}
      Const
        nItems = 5;
      var
        nBasicsIntArray : array [0..(nItems - 1)] of integer;
        nBasicsStrArray : array [0..(nItems - 1)] of string;
    public
      { Public declarations }
  end;

When I run the application it says "'END' expected but 'CONST' found". 当我运行该应用程序时,它会显示“期望'END'但找到'CONST'”。 I am using Windows 7 and Delphi 7 and I have not had this problem before. 我正在使用Windows 7和Delphi 7,以前没有遇到过此问题。

It could honestly be that I'm missing something stupid but I've overlooked everything and I can't seem to find the cause of the problem. 老实说这可能是我错过了一些愚蠢的事情,但是我却忽略了一切,而且我似乎找不到问题的根源。

The ability to declare constants inside a class was not supported in Delphi 7. That language feature was added in a later release. 在Delphi 7中不支持在类中声明常量的功能。该语言功能是在以后的版本中添加的。

Declare your constant outside the class. 在课堂外宣告你的常数。

The same is true of your use of var . 您对var的使用也是如此。 That syntax is not supported in Delphi 7. You should simply remove the var keyword from your class declaration. Delphi 7不支持该语法。您只需从类声明中删除var关键字即可。

As written above, a solution can be to move your local constant to global position under Delphi 7. Here is a code sample: 如上所述,一种解决方案是将您的局部常量移动到Delphi 7下的全局位置。这是一个代码示例:

Const
    nItems = 5;
type
  TForm1 = class(TForm)
    edtDateTime: TEdit;
//...
  private
      nBasicsIntArray : array [0..(nItems - 1)] of integer;
      nBasicsStrArray : array [0..(nItems - 1)] of string;
//...
  end;

Also you can use " const " and " type " keywords multiple times to keep things together. 您也可以多次使用“ const ”和“ type ”关键字来使事物保持一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM