procedure TForm1.btn1Click(Sender: TObject);
var
a:Variant;
begin
if VarIsNull(a) then
ShowMessage('VarIsNull=true');
if VarIsEmpty(a) then
ShowMessage('VarIsEmpty=true'); //这里执行
a:=vaNull;
if VarIsNull(a) then
ShowMessage('a:=vaNull,VarIsNull=true');
if VarIsEmpty(a) then
ShowMessage('a:=vaNull,VarIsEmpty=true');
a:=Unassigned;
if VarIsNull(a) then
ShowMessage('a:=Unassigned,VarIsNull=true');
if VarIsEmpty(a) then
ShowMessage('a:=Unassigned,VarIsEmpty=true'); //这里执行
end;测试环境,win7,64位,delphi7大家发上自己的测试情况 看看。。
var
a:Variant;
begin
if VarIsNull(a) then
ShowMessage('VarIsNull=true');
if VarIsEmpty(a) then
ShowMessage('VarIsEmpty=true'); //这里执行
a:=vaNull;
if VarIsNull(a) then
ShowMessage('a:=vaNull,VarIsNull=true');
if VarIsEmpty(a) then
ShowMessage('a:=vaNull,VarIsEmpty=true');
a:=Unassigned;
if VarIsNull(a) then
ShowMessage('a:=Unassigned,VarIsNull=true');
if VarIsEmpty(a) then
ShowMessage('a:=Unassigned,VarIsEmpty=true'); //这里执行
end;测试环境,win7,64位,delphi7大家发上自己的测试情况 看看。。
看上面的Delphi帮助。
VarIsNull是确定Variant变量是不是包含一个NULL值,这个变量是有值的,不过只是NULL,这时这个函数返回True;
VarIsEmpty是确定Variant变量是不是有值,没有赋过值,或者是“赋”了一个“Unassigned”,这时这个函数返回True;
这里应是:
a := Null;
Null:VType := varNull{varNull的值为1}
Unassigned:VType := varEmpty{varEmpty的值为0}
VarIsNull:Result := VType = varNull
VarIsEmpty:Result := VType = varEmpty
另外,当为Variant类型的局部变量开辟内存空间时,似乎将其值填充为0(这个是从汇编代码中猜测的,Delphi官方文档中没看到)
有了上面的说明,楼主测试的结果就容易理解了——
刚进去,由于VType为0,因此,VarIsEmpty成立;
将a设为vaNull,由于vaNull为枚举类型,值为0,Delphi将其视为整数处理,将VType设为varSmallint(这个具体没测试,还有可能为varInteger或varShortInt吧),因此,两个判断都不成立(其实,如果这时将a的VType设为varNull,例如,TVarData(v).VType := varNull,则VarIsNull会成立);
而将a设为Unassigned后的结果就留给楼主自己吧,呵呵。
var
a:Variant;
begin
if VarIsNull(a) then
ShowMessage('VarIsNull=true');
if VarIsEmpty(a) then
ShowMessage('VarIsEmpty=true'); //执行 a:=vaNull;
if VarIsNull(a) then
ShowMessage('a:=vaNull,VarIsNull=true');
if VarIsEmpty(a) then
ShowMessage('a:=vaNull,VarIsEmpty=true'); a:=Unassigned;
if VarIsNull(a) then
ShowMessage('a:=Unassigned,VarIsNull=true');
if VarIsEmpty(a) then
ShowMessage('a:=Unassigned,VarIsEmpty=true'); //执行 a:=null;
if VarIsNull(a) then
ShowMessage('a:=null,VarIsNull=true'); //执行
if VarIsEmpty(a) then
ShowMessage('a:=null,VarIsEmpty=true');end;