delphi mysql变体类型_variant 类型如何使用(100分)

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.

TargetSourceintegerrealstringcharacterBoolean

integer

converts integer formatsconverts to realconverts to string representationsame as string (left)returns False if 0, True otherwise

real

rounds to nearest integerconverts real formatsconverts to string representation using Windows regional settingssame as string (left)returns False if 0, True otherwise

string

converts to integer, truncating if necessary; raises exception if string is not numericconverts to real using Windows regional settings; raises exception if string is not numericconverts string/character formatssame 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-stringsame 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 0returns 0returns empty stringsame as string (left)returns False

Null

raises exceptionraises exceptionraises exceptionsame 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
delphi 为了完全支持OLE,32位Delphi 增加了Variant 数据类型,本节将从宏观角度来析这种数据类型。实际上,Variant类型对Pascal语言有普遍而深入的影响,Delphi 控件库中与OLE 无关的地方也使用到这种类型Variant变量没有类型 一般说来,你可以用Variant 变量存储任何数据类型,对它执行各种操作和类型转换。需要注意的是:这违反了Pascal 语言的一贯原则,有悖于良好的编程习惯。variant 变量的类型检查和计算在运行期间才进行,编译器不会提示代码中的潜在错误,这些错误在进一步测试中才能发现。总之,你可以认为包含variant变量的代码是解释性代码,正如解释性代码一样,许多操作直到执行时才能知道,这对代码运行速度会有很大的影响。 上面对Variant 类型使用提出了警告,现在来看看Variant 类型究竟能干什么。基本上说,如果声明了一个variant 变量: var V: Variant; 你就可以把各种不同类型的值赋给它: V := 10; V := 'Hello, World'; V := 45.55; 一旦得到一个variant 值,你可以把它拷贝给任何兼容或不兼容的数据类型。如果你把值赋给不兼容的数据类型Delphi 会力尽所能进行转换,无法转换则颁布一个运行时间错误。实际上,variant变量中不仅包含了数据还包含有类型信息,并允许一系列运行时间操作,这些操作很方便,但运行速度慢且安全性差。 见例VariTest,它是上面代码的扩展。窗体上有三个编辑框,一对按钮,第一个按钮的OnClick 事件代码如下: procedure TForm1.Button1Click(Sender: TObject); var V: Variant; begin V := 10; Edit1.Text := V; V := 'Hello, World'; Edit2.Text := V; V := 45.55; Edit3.Text := V; end; 很有趣是不是?你可以把一个值为字符串的variant 变量赋给编辑框Text 属性,还可以把值为整数或浮点数的variant 变量赋给Text属性。正如你在图10.1中所看到的,一切正常。 (图10.1)按Assign按钮后,例VariTest的输出结果 图 10.1: 例 VariTest 的 Assign 按钮 Click 事件输出结果 更糟糕的是:你还可以用variant变量计算数值,从第二个按钮的Click事件代码就可看到这一点: procedure TForm1.Button2Click(Sender: TObject); var V: Variant; N: Integer; begin V := Edit1.Text; N := Integer(V) * 2; V := N; Edit1.Text := V; end; 至少这种代码带有一定危险性,如果第一个编辑框包含了一个数字,那么一切运行正常;如果不是,将会引发异常。这里再重申一遍,如果不到万不得以,不要随便使用Variant 类型,还是应坚持使用传统的Pascal 数据类型类型检查方法。在Delphi 和 VCL中,variant变量主要是用于 OLE 支持和数据库域的访问。 Variant类型内部结构 Delphi中定义了一个 variant 记录类型,TVarData,它与Variant 类型有相同的内存布局。你可以通过TVarData访问variant变量的实际类型。TVarData 结构中包含了Variant类型信息(由Vtype域表示)、一些保留域及当前值。 VType域的取值包括OLE 自动化中的所有数据类型,这些类型通常叫OLE 类型variant 类型。以下是variant 类型的完整列表,按字母顺序排列: varArray varBoolean varByRef varCurrency varDate varDispatch varDouble varEmpty varError varInteger varNull varOleStr varSingle varSmallint varString varTypeMask varUnknown varVariant 你可以在Delphi 帮助系统的variants 主题下找到这些类型的说明。 还有许多操作variant 变量的函数,你可以用它们进行特定的类型转换,或通过它们获取variant变量的类型信息(例如VarType 函数),当你用variant变量写表达式时,Delphi会自动调用这些类型转换和赋值函数。另外还有操作variant 数组的例程,你可以通过帮助文件的Variant support routines 主题了解相关内容。 Variant类型运行很慢! Variant 类型代码运行很慢,不仅数据类型转换如此,两个值为整数的Variant 变量相加也是如此。它们几乎跟Visual Basic这种解释性代码一样慢!为了比较Variant变量和整型变量的运行速度,请看例VSpeed 。 程序中设置了一个循环,记录运行时间并在进程条中显示运行状态。下面是基于variant类型的一段代码,基于整型的代码与此相似: procedure TForm1.Button1Click(Sender: TObject); var time1, time2: TDateTime; n1, n2: Variant; begin time1 := Now; n1 := 0; n2 := 0; ProgressBar1.Position := 0; while n1 < 5000000 do begin n2 := n2 + n1; Inc (n1); if (n1 mod 50000) = 0 then begin ProgressBar1.Position := n1 div 50000; Application.ProcessMessages; end; end; // we must use the result Total := n2; time2 := Now; Label1.Caption := FormatDateTime ( 'n:ss', Time2-Time1) + ' seconds'; end; 记时这段代码值得一看,因为你可以把它用到任何类型的性能测试中。正如你所看到的,程序用Now 函数获取当前的时间,用FormatDateTime 函数格式化时间差,输出结果以("n")和秒("ss")表示。除此之外,你可以用Windows API的GetTickCount 函数,该函数能精确显示操作系统启动后至当前的毫秒数。 从上例可见两者的速度差异非常之大,以至于不用精确记时也能看到这种差异。图10.2是在本人计算机上运行程序看到的结果。当然运行结果取决于运行程序的计算机,但是两者的数值比不会有太大变化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值