type
  Dish = record
    Name :string;
    Price :Integer;
  end;  Dishes = array[0..3] of Dish;
  
  ... = class
    ...
    private
      //在这里初始化一个数组可以吗?我看到一本书是这样的,可是我这里编译不能通过
      ADishes :Dish = 
        ((Name:...;Price:...),
         (Name:.............),
         ...
         ...                ));
  ...
  end;另外,我特别想写成这样
去掉Dish的定义部分。
然后数组定义成这样://Dishes不在是一个类型了,而成了一个变量。
Dishes :array[0..3] of Dish=((...);(...);(...);(...););
这样写可以吗?
因为我定义一个数组,却要付出好几个英文单词。痛苦。

解决方案 »

  1.   

    type
      Dish = record
        Name :string;
        Price :Integer;
      end;
    var Dishes:array[0..3] of Dish;
    初始化:
    var i :integer;
    for i:=0 to 3 do 
    begin
        Dishes[i].Name:='yourname+inttostr(i)';
        Dishes[i].price:=20*I;
    end;
      

  2.   

    procedure TForm1.FormCreate(Sender: TObject);
    type
      Dish = record
        Name :string;
        Price :Integer;
      end;
    var Dishes:array[0..3] of Dish;
    //初始化:
     i :integer;
     begin
    for i:=0 to 3 do 
    begin
        Dishes[i].Name:='yourname+inttostr(i)';
        Dishes[i].price:=20*I;
    end;
      end;
    end.
    在我这里一点问题也没有啊,怎么不行?
      

  3.   

    Digits: array[0..9] of Char = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
      

  4.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      Dish = record
        Name :string;
        Price :Integer;
      end;type
      TForm1 = class(TForm)
        Button1: TButton;
        Memo1: TMemo;
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}var Dishes:array[0..3] of Dish;
    procedure TForm1.Button1Click(Sender: TObject);
    var i: integer;
    begin
       memo1.Clear;
       for i:= low(dishes) to High(dishes) do
          Memo1.Lines.Add('Name:' + dishes[i].Name + ';'+ ' Price:' + Inttostr(dishes[i].Price));
    end;procedure TForm1.FormCreate(Sender: TObject);
    var i :integer;
    begin
       //初始化:
       for i:=0 to 3 do
       begin
           Dishes[i].Name:='yourname+inttostr(i)';
           Dishes[i].price:=20*I;
       end;
    end;end.
      

  5.   

    作为变量或常量时可以这样写,但作为类成员时不可以。
    var
      Dishes:array [0..1] of dish=((name:'aa';price:10),(name:'bb';price:20));
      

  6.   

    同意hiflower(花)的看法
    delphi毕竟不是c++