这样定义数组可以吗?
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=((...);(...);(...);(...););
这样写可以吗?
因为我定义一个数组,却要付出好几个英文单词。痛苦。
问题点数:0、回复次数:10Top
1 楼functionreal(funcreal)回复于 2003-12-03 15:52:51 得分 0
upTop
2 楼ideallover21(8258660)回复于 2003-12-03 21:50:02 得分 0
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;
Top
3 楼VisualLion(狮子)回复于 2003-12-04 08:48:41 得分 0
试过了,好像不行。:<Top
4 楼ideallover21(8258660)回复于 2003-12-04 09:31:15 得分 0
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.
在我这里一点问题也没有啊,怎么不行?Top
5 楼littlevoice(这个冬天不太冷)回复于 2003-12-16 17:34:28 得分 0
楼主的想法好象不可能实现吧,没见过,用结构不是很清晰吗Top
6 楼hexenzhou(甲骨文)回复于 2003-12-26 14:41:11 得分 0
只有在全局变量初始化的时候才可以那样写,其他的好像不行。Top
7 楼VisualLion(狮子)回复于 2003-12-29 11:10:01 得分 0
Digits: array[0..9] of Char = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');Top
8 楼Hedonism(江悦)回复于 2003-12-29 13:20:01 得分 0
unit Unit1;
interface
uses
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.
Top
9 楼hiflower(花)回复于 2003-12-29 13:27:01 得分 0
作为变量或常量时可以这样写,但作为类成员时不可以。
var
Dishes:array [0..1] of dish=((name:'aa';price:10),(name:'bb';price:20));
Top
10 楼MooseWOler(Mr.Oler)回复于 2003-12-30 15:37:25 得分 0
同意hiflower(花)的看法
delphi毕竟不是c++
Top




