zoukankan      html  css  js  c++  java
  • delphi assigned函数的用法

    if not Assigned(Modeless) then Assigned()什么意思!

    assigned 是用来判断某一指针(pointer)或过程引用是否为nil(空),如果为空则返回假(false)。

    用法示例(防止窗体被实例化多次):

    unit Unit1;

    interface

    uses

      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

      Dialogs, StdCtrls;

    type

      TForm1 = class(TForm)

        Button1: TButton;

        procedure Button1Click(Sender: TObject);

      private

        { Private declarations }

      public

        { Public declarations }

      end;

    var

      Form1: TForm1;

    implementation

    uses Unit2;

    {$R *.dfm}

    procedure TForm1.Button1Click(Sender: TObject);

    begin

      if (Not assigned(form2)) then

      begin

        form2:=Tform2.Create(Self);

      end;

      form2.show;

    end;

    end.

    -----------------------

    unit Unit2;

    interface

    uses

      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

      Dialogs;

    type

      TForm2 = class(TForm)

        procedure FormClose(Sender: TObject; var Action: TCloseAction);

      private

        { Private declarations }

      public

        { Public declarations }

      end;

    var

      Form2: TForm2;

    implementation

    {$R *.dfm}

    procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);

    begin

      Action:=caFree;

      form2:=nil;  //这句比较重要,窗体被释放时,窗体变量并不会自动变空

    end;

    end.

    很详细呢,不过不是我的原创,我转的,希望对你有用啦。。。。。

    另一篇网络文章

    关于Delphi中的Assigned
    2008-04-05 10:53

    关于Delphi中的Assigned
     
    function Assigned(var P): Boolean;

    Description

    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.

    Assigned returns False if P is nil, True otherwise.

    检查指针指向的参考变量或过程是否为nil

    每次我通常的处理方法都是:

    if assigned(frm) then frm.close;    但是当下次调用时就会出错。为什么呢,直到咋天我才知道原因

    frm.close;frm.free;   只是指定这块内存可以重写,并未释放为NIL 因此当下次调用时即使frm.free已经

    执行过assigned(frm)仍为TRUE;

    正确的处理方法:

    if assigned(frm) then
    begin
        frm.close;
        frm:=nil;
    end;

    或:

    if assigned(frm) then
    begin
       frm.close;
       freeandnil(frm);
    end;

    freeandnil的说明:

    procedure FreeAndNil(var Obj);

    Description

    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.

  • 相关阅读:
    SolidEdge如何绘制阵列之后取消掉某一些
    SolidEdge如何绘制变化半径倒圆角
    SolidEdge如何复制特征 建立类似于UG 块的概念
    SolidEdge如何打开或关闭自动标注尺寸
    SolidEdge 装配体中如何快速的搞定一个面上所有螺丝 如何在装配体上进行阵列
    SolidEdge 如何由装配图快速生成爆炸视图
    SolidEdge 如何由装配图快速进行标注和零件序号编写 制作BOM表
    [Angular 8] Take away: Web Components with Angular Elements: Beyond the Basics
    [Angular 8] Take away: Tools for Fast Angular Applications
    [Algorithm] Calculate Pow(x,n) using recursion
  • 原文地址:https://www.cnblogs.com/jijm123/p/10818999.html
Copyright © 2011-2022 走看看