检查变量是否为零的最佳方式? [英] Best way to check if a Variable is nil?
本文介绍了检查变量是否为零的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所有程序应该做的一个常见的条件是检查变量是否被分配。
A common condition that all programs should do is to check if variables are assigned or not.
采取以下语句:
(1)
if Assigned(Ptr) then
begin
// do something
end;
(2)
if Ptr <> nil then
begin
// do something
end;
Assigned(Ptr)
和 Ptr<> nil
?
推荐答案
通常是一样的...除了你检查一个函数...
It's usually the same... except when you check a function...
function mfi: TObject;
begin
Result := nil;
end;
procedure TForm1.btn1Click(Sender: TObject);
type
TMyFunction = function: TObject of object;
var
f: TMyFunction;
begin
f := mfi;
if Assigned(f) then
begin
ShowMessage('yes'); // TRUE
end
else
begin
ShowMessage('no');
end;
if f <> nil then
begin
ShowMessage('yes');
end
else
begin
ShowMessage('no'); // FALSE
end;
end;
使用第二个语法,它将检查函数的结果,而不是函数本身...
With the second syntax, it will check the result of the function, not the function itself...
这篇关于检查变量是否为零的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文