variant 类型如何使用(100分)

  • 主题发起人 小老弟
  • 开始时间

小老弟

Unregistered / Unconfirmed
GUEST, unregistred user!
当我把一条数据库纪录 放入 variant类型的变量v中时,我用v[0] 时出错
提示
没有发现 数组
 
你是怎么放到变量v中的,我试过了,没有任何问题呀!你再说清楚一点!
 
variant类型不是数组啊!
 
variant是变体类型,可以接受任何类型的赋值
不是变体数组,除非你定义一个variant类型的数组才能那样用
 
如何定义 variant数组
 
variant数组
Object Pascal提供了若干支持variant数组的函数,允许建立variant数组:
1、vararraycreate()函数。
如:var v:variant;
begin
v:=vararraycreate([1,2],varinterger);//// 创建一个2个元素的数组
v[1]:=1;
v[2]:=2;
2、vararrayof()函数。
这个函数创建一个一维数组,它的元素由values参数指定
如:v:=vararrayof([1,2,3]);
所以:
v[1]:=1;
v[2]:=2;
v[3]:=3;
这是静态的数组

 
All integer, real, string, character, and Boolean types (except Int64) are assignment-compatible with Variant. Expressions can be explicitly cast as variants, and the VarAsType and VarCast standard routines can be used to change the internal representation of a variant. The following code demonstrates the use of variants and some of the automatic conversions performed when variants are mixed with other types.

var

V1, V2, V3, V4, V5: Variant;
I: Integer;
D: Double;
S: string;
begin
V1 := 1; { integer value }
V2 := 1234.5678; { real value }
V3 := 'Hello world!'; { string value }
V4 := '1000'; { string value }
V5 := V1 + V2 + V4; { real value 2235.5678}
I := V1; { I = 1 (integer value) }
D := V2; { D = 1234.5678 (real value) }
S := V3; { S = 'Hello world!' (string value) }
I := V4; { I = 1000 (integer value) }
S := V5; { S = '2235.5678' (string value) }

end;

The compiler performs type conversions according to the following rules.

TargetSource integer real string character Boolean
integer
converts integer formats converts to real converts to string representation same as string (left) returns False if 0, True otherwise
real


rounds to nearest integer converts real formats converts to string representation using Windows regional settings same as string (left) returns False if 0, True otherwise
string





converts to integer, truncating if necessary; raises exception if string is not numeric converts to real using Windows regional settings; raises exception if string is not numeric converts string/character formats same as string (left) returns False if string is 揻alse?(non朿ase-sensitive) or a numeric string that evaluates to 0, True if string is 搕rue?or a nonzero numeric string; raises exception otherwise
character
same as string (above) same as string (above) same as string (above) same as string-to-string same as string (above)
Boolean
False = 0,
True = ?
(255 if Byte) False = 0,
True = ? False = ??
True = 摉1?same as string (left) False = False,
True = True
Unassigned
returns 0 returns 0 returns empty string same as string (left) returns False
Null
raises exception raises exception raises exception same as string (left) raises exception
Out-of-range assignments often result in the target variable getting the highest value in its range. Invalid assignments or casts raise the EVariantError exception.
Special conversion rules apply to the TDateTime real type declared in the System unit. When a TDateTime is converted to any other type, it treated as a normal Double. When an integer, real, or Boolean is converted to a TDateTime, it is first converted to a Double, then read as a date-time value. When a string is converted to a TDateTime, it is interpreted as a date-time value using the Windows regional settings. When an Unassigned value is converted to TDateTime, it is treated like the real or integer value 0. Converting a Null value to TDateTime raises an exception.

If a variant references a COM object, any attempt to convert it reads the object抯 default property and converts that value to the requested type. If the object has no default property, an exception is raised.
 
顶部