delphi assigned函数的用法
<pre>if not Assigned(Modeless) then Assigned()什么意思!</pre><p> </p>
<p>assigned 是用来判断某一指针(pointer)或过程引用是否为nil(空),如果为空则返回假(false)。</p>
<p> </p>
<p>用法示例(防止窗体被实例化多次):</p>
<p> </p>
<p> </p>
<p>unit Unit1;</p>
<p> </p>
<p>interface</p>
<p> </p>
<p>uses</p>
<p> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,</p>
<p> Dialogs, StdCtrls;</p>
<p> </p>
<p>type</p>
<p> TForm1 = class(TForm)</p>
<p> Button1: TButton;</p>
<p> procedure Button1Click(Sender: TObject);</p>
<p> private</p>
<p> { Private declarations }</p>
<p> public</p>
<p> { Public declarations }</p>
<p> end;</p>
<p> </p>
<p>var</p>
<p> Form1: TForm1;</p>
<p> </p>
<p>implementation</p>
<p> </p>
<p>uses Unit2;</p>
<p> </p>
<p>{$R *.dfm}</p>
<p> </p>
<p>procedure TForm1.Button1Click(Sender: TObject);</p>
<p>begin</p>
<p> if (Not assigned(form2)) then</p>
<p> begin</p>
<p> form2:=Tform2.Create(Self);</p>
<p> end;</p>
<p> form2.show;</p>
<p>end;</p>
<p> </p>
<p>end.</p>
<p> </p>
<p>-----------------------</p>
<p>unit Unit2;</p>
<p> </p>
<p>interface</p>
<p> </p>
<p>uses</p>
<p> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,</p>
<p> Dialogs;</p>
<p> </p>
<p>type</p>
<p> TForm2 = class(TForm)</p>
<p> procedure FormClose(Sender: TObject; var Action: TCloseAction);</p>
<p> private</p>
<p> { Private declarations }</p>
<p> public</p>
<p> { Public declarations }</p>
<p> end;</p>
<p> </p>
<p>var</p>
<p> Form2: TForm2;</p>
<p> </p>
<p>implementation</p>
<p> </p>
<p>{$R *.dfm}</p>
<p> </p>
<p>procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);</p>
<p>begin</p>
<p> Action:=caFree;</p>
<p> form2:=nil; //这句比较重要,窗体被释放时,窗体变量并不会自动变空</p>
<p>end;</p>
<p> </p>
<p>end.</p>
<p> </p>
<p>很详细呢,不过不是我的原创,我转的,希望对你有用啦。。。。。</p>
<p> </p>
<p>另一篇网络文章</p>
<div>关于Delphi中的Assigned</div>
<div>2008-04-05 10:53</div>
<p></p>
<table>
<tbody>
<tr>
<td>
<div>关于Delphi中的Assigned</div>
<div> </div>
<table>
<tbody>
<tr>
<td>function Assigned(var P): Boolean;<br><br>Description<br><br>Use Assigned to determine whether the pointer or procedure referenced by P is nil. P must be a variable reference of a pointer or procedural type. Assigned(P) corresponds to the test P<> nil for a pointer variable, and @P <> nil for a procedural variable.<br><br>Assigned returns False if P is nil, True otherwise.<br><br>检查指针指向的参考变量或过程是否为nil<br><br>每次我通常的处理方法都是:<br><br>if assigned(frm) then frm.close; 但是当下次调用时就会出错。为什么呢,直到咋天我才知道原因<br><br>frm.close;frm.free; 只是指定这块内存可以重写,并未释放为NIL 因此当下次调用时即使frm.free已经<br><br>执行过assigned(frm)仍为TRUE;<br><br>正确的处理方法:<br><br>if assigned(frm) then<br>begin<br> frm.close;<br> frm:=nil;<br>end;<br><br>或:<br><br>if assigned(frm) then<br>begin<br> frm.close;<br> freeandnil(frm);<br>end;<br><br>freeandnil的说明:<br><br>procedure FreeAndNil(var Obj);<br><br>Description<br><br>Use FreeAndNil to ensure that a variable is nil after you free the object it references. Pass any variable that represents an object as the Obj parameter.<br><br></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
<div id="MySignature" role="contentinfo">
好的代码像粥一样,都是用时间熬出来的<br><br>
来源:https://www.cnblogs.com/jijm123/p/10818999.html
頁:
[1]