现在的位置: 首页 > 综合 > 正文

如何初始化数组和记录

2013年09月10日 ⁄ 综合 ⁄ 共 938字 ⁄ 字号 评论关闭

delphi提供一个fillchar方法,使用它可以将数组初始化。

1.建一项目,向其中添加一个bottom控件

2.在该控件的onclick事件中添加代码如下:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
type
TIntRec = record //定义记录
i1: integer;
i2: integer;
i3: integer;
i4: integer;
i5: integer;
end;
var
IntArray: array[0..4] of integer; //定义数组
i: integer;
s: string;
IntRec: TIntRec;
begin
//初始化数组
//如果没有下面一行代码,那么数组的值为随机数
FillChar(IntArray,SizeOf(IntArray),0);
for i := low(IntArray) to high(IntArray) do
s := s+IntToStr(IntArray);
form1.Caption := 'IntArray: '+s;

//初始化记录
//如果没有下面一行代码,那么记录的值为随机数
FillChar(IntRec,SizeOf(IntRec),0);
form1.Caption := form1.Caption+' '+'IntRec: '+IntToStr(IntRec.i1)+IntToStr(IntRec.i2)+IntToStr(IntRec.i3)+IntToStr(IntRec.i4)+IntToStr(IntRec.i5);
end;

end.

抱歉!评论已关闭.