DFW确实是一个可以使人增长知识的地方,我加入该论坛也只有1个月多的时间,但是学到了很多知识,本贴我就学到了好多知识。真是应该感谢很多人。
本贴的问题讨论了这么长时间,后续又产生了一些新问题,甚至我们都忘了题目,问题是:“怎样将一个数组全部置为空值?”然后补充说明为:
=================================================
我需要这样一个函数:
var: value:?array?of?variant
函数名(value, null, 开始下标, 个数)
比如我要置一个长度20的数组为空:
函数(value, null, 0, 20);
笨办法:
for i:= 0 to 19 do
value := null
=================================================
刚才,我看了Delphi的帮助文件,以下既是帮助文件的全部内容和例子:
Unassigned function
See also Delphi example C++ example
--------------------------------------------------------
Returns an "empty" variant.
Unit
Variants
Category
Variant support routines
Delphi syntax:
function Unassigned: Variant;
C++ syntax:
extern PACKAGE Variant __fastcall Unassigned();
Description
A variant variable can be "empty", meaning it has not yet been assigned to. The Unassigned function returns an empty variant, which can be assigned to a variant variable to restore the variable to its initial state.
Use the VarIsEmpty function to test whether a variant is empty. When used on an empty variant, the VarType standard function returns varEmpty.
If an empty variant is cast to another type (for example, by assigning to a non-variant variable or calling VarAsType) the following conversions occur:
Destination Conversion result
Numeric type Zero
String type Empty string
Boolean False.
Unassigned is useful with variants referencing OLE Automation Objects that you want to keep "alive" until another value is assigned to the variant.
-----------------------------------------------------------------------------------------------------------------------------------------------------
Unassigned example
In the code fragment shown below, the statement that assigns Unassigned to the MSWord variable causes the OLE Automation Object that was created to interface with Word to be released.
var
MSWord: Variant;
begin
...
MSWord := CreateOleObject('Word.Basic');
...
MSWord := Unassigned;
...
end;
下面的内容是来自Delphi的Variants.pas单元文件
function Unassigned: Variant;
begin
_VarClear(TVarData(Result));
end;
procedure _VarClear(var V: TVarData);
begin
// byrefs and those inline data types are easy
if (V.VType and varDeepData) = 0 then
V.VType := varEmpty
else
VarClearDeep(V);
end;
procedure VarClearDeep(var V: TVarData);
var
LHandler: TCustomVariantType;
begin
// quick test for the simple ones
if (V.VType < varInt64) then
VarResultCheck(VariantClear(V))
// clear the pascal string correctly for reference counting
else if V.VType = varString then
begin
V.VType := varEmpty;
String(V.VString) := '';
end
// let CORBA deal with its own type
else if V.VType = varAny then
ClearAnyProc(V)
// custom handle the arrays
else if (V.VType and varArray) <> 0 then
VarArrayClear(V)
// ok, finally is it a custom variant type?
else if FindCustomVariantType(V.VType, LHandler) then
LHandler.Clear(V)
// finally let the OS attempt to deal with it
else
VarResultCheck(VariantClear(V));
end;
小雨哥提到的VarArrayOf的原形,内部机制也是用循环赋值的:
function VarArrayOf(const Values: array of Variant): Variant;
var
I: Integer;
begin
Result := VarArrayCreate([0, High(Values)], varVariant);
for I := 0 to High(Values) do
Result := Values;
end;
还有一些内容,其实大家可以参考Variants单元
因此将长度为20的Variant类型的数组置为空,下面的写法和Delphi的写法没有什么本质区别
for i:= 0 to 19 do
value := Unassigned;