var
MyArray:Variant;
MyArray:=VarArrayCreate([0,1,2,5],Varinteger)
这个中的[0,1,2,5]解释一下,从这里可以看出这是一个二维数组,
0是一维的最小下标,1是一维的最大下标;
2是二维的最小下标,5是二维的最大下标;
---------------------------------------------
MyArray:=VarArrayCreate([0,1,2,5,6,10],Varinteger)
这个中的[0,1,2,5,6,10]解释一下,从这里可以看出这是一个三维数组,
0是一维的最小下标,1是一维的最大下标;
2是二维的最小下标,5是二维的最大下标;
6是三维的最小下标,10是三维的最大下标;
----------------------------------------------
想必已经看出规律了!一对一对的出现;四维、五维.....数组以此类推,但维数不要太多,VarArrayCreate不能创建超过64维的数组;
VarArrayCreate有两个参数:第一个是数组,第二个是类型(这里的类型不能是Integer,而是Varinteger);
--------------------------------
System单元中有定义
varEmpty = $0000; { vt_empty 0 }
varNull = $0001; { vt_null 1 }
varSmallint = $0002; { vt_i2 2 }
varInteger = $0003; { vt_i4 3 }
varSingle = $0004; { vt_r4 4 }
varDouble = $0005; { vt_r8 5 }
varCurrency = $0006; { vt_cy 6 }
varDate = $0007; { vt_date 7 }
varOleStr = $0008; { vt_bstr 8 }
varDispatch = $0009; { vt_dispatch 9 }
varError = $000A; { vt_error 10 }
varBoolean = $000B; { vt_bool 11 }
varVariant = $000C; { vt_variant 12 }
varUnknown = $000D; { vt_unknown 13 }
//varDecimal = $000E; { vt_decimal 14 } {UNSUPPORTED as of v6.x code base}
//varUndef0F = $000F; { undefined 15 } {UNSUPPORTED per Microsoft}
varShortInt = $0010; { vt_i1 16 }
varByte = $0011; { vt_ui1 17 }
varWord = $0012; { vt_ui2 18 }
varLongWord = $0013; { vt_ui4 19 }
varInt64 = $0014; { vt_i8 20 }
//varWord64 = $0015; { vt_ui8 21 } {UNSUPPORTED as of v6.x code base}
{ if adding new items, update Variants' varLast, BaseTypeMap and OpTypeMap }
varStrArg = $0048; { vt_clsid 72 }
varString = $0100; { Pascal string 256 } {not OLE compatible }
varAny = $0101; { Corba any 257 } {not OLE compatible }
// custom types range from $110 (272) to $7FF (2047)
varTypeMask = $0FFF;
varArray = $2000;
varByRef = $4000;
-------------
怎么看到System?为啥没有Uses也可以用?
System不用添加,编译器会自动添加,所以在编译的时候,我们手动的Uses System会报错;
想要查看System,在单元中Uses System,然后Ctrl+鼠标左键单击System;就能进入该单元;但是在编译之前记得把System从Uses中移除,否则会报错!
--------------------------------
举例:VarArrayCreate([0,1,2,5,6,10],Varinteger)
这里只验证[0,1,2,5,6,10]这些数字,这些数字只是下标,这个三维数组有多少元素(2*4*5=40);
------------------------------------Unit 开始
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
array01:Variant;
i,j,k:Integer;
Dim:Word; //维数
begin
i:=0;
j:=0;
K:=0;
array01:=VarArrayCreate([0,1,2,5,6,10],varInteger);
//Dim:=VarArrayDimCount(array01);
//ShowMessage(IntToStr(Dim));
Memo1.Lines.Add('----------一维------------');
for i:=VarArrayLowBound(array01,1) to VarArrayHighBound(array01,1) do
begin
Memo1.Lines.Add(IntToStr(i));
end;
Memo1.Lines.Add('----------二维------------');
for i:=VarArrayLowBound(array01,2) to VarArrayHighBound(array01,2) do
begin
Memo1.Lines.Add(IntToStr(i));
end;
Memo1.Lines.Add('----------三维------------');
for i:=VarArrayLowBound(array01,3) to VarArrayHighBound(array01,3) do
begin
Memo1.Lines.Add(IntToStr(i));
end;
end;
end.
-------------------------------------Unit 结束
---------------------Form 开始
object Form1: TForm1
Left = 739
Top = 184
Width = 417
Height = 595
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Memo1: TMemo
Left = 24
Top = 16
Width = 185
Height = 457
ImeName = '中文(简体) - 搜狗拼音输入法'
ScrollBars = ssBoth
TabOrder = 0
end
object Button1: TButton
Left = 256
Top = 200
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 1
OnClick = Button1Click
end
end
---------------------Form 结束