例如:property IsTrue: Boolean Read FIsTrue Write FIsTrue default True;
我还是要先设置FIsTrue :=  True后IsTrue值才是True的,这不是说,上述default True没起作用了?
先谢了!

解决方案 »

  1.   

    to:bonniewater(陪你去看海) 
       你是说“要先设置FIsTrue :=  True后IsTrue值才是True的”这不可能?
       我试了就是这样子的啊
      

  2.   

    如果是默认值,dfm窗体文件中可以不保存具体的值
      

  3.   

    我建了个类:
    TTest=Class(TComponent)
    private
      FIsTrue :Boolean;
    public
      property IsTrue: Boolean Read FIsTrue Write FIsTrue default True;
      constructor Create(AOwner:TComponent);override;
    end;constructor TTest.Create(AOwner:TComponent);
    begin
      inherited Create(AOwner); 
      FIsTrue :=True;
    end;测试:
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      test:=TTest.Create(Self);
      if test.IsTrue then
        showmessage('right');
    end;结果发现如果TTest.Create里没有FIsTrue :=True的话,showmessage('right')不会执行的。问题何在?请各位指点!
      

  4.   

    When you declare a property, you can specify a default value for it. Delphi uses the default value to determine whether to store the property in a form file. If you do not specify a default value for a property, Delphi always stores the property.
    To specify a default value for a property, append the default directive to the property抯 declaration (or redeclaration), followed by the default value. For example,property Cool Boolean read GetCool write SetCool default True;Note: Declaring a default value does not set the property to that value. The component抯 constructor method should initialize property values when appropriate. However, since objects always initialize their fields to 0, it is not strictly necessary for the constructor to set integer properties to 0, string properties to null, or Boolean properties to False.