RAD Studio |  |

Specifying the New Default Property Value

When Delphi stores a description of a form in a form file, it stores the values only of properties that differ from their defaults. Storing only the differing values keeps the form files small and makes loading the form faster. If you create a property or change the default value, it is a good idea to update the property declaration to include the new default. Form files, loading, and default values are explained in more detail in Making components available at design time.

To change the default value of a property, redeclare the property name, followed by the directive default and the new default value. You don't need to redeclare the entire property, just the name and the default value.

For the word-wrapping memo component, you redeclare the WordWrap property in the published part of the object declaration, with a default value of False:

Copy Code

    type
      TWrapMemo = class(TMemo)
      .
      .
      .
      published
        property WordWrap default False;
      end;

Copy Code

    //header file
    class PACKAGE TYellowMemo : public TMemo
    {
    public:
        virtual __fastcall TYellowMemo(TComponent* Owner);
    __published:
        __property Color = {default=clYellow};
    };

Copy Code

    //implmentation file
    __fastcall TYellowMemo::TYellowMemo(TComponent* AOwner) : TMemo(AOwner)
    {
      Color = clYellow;
      WordWrap = false;
    }

Copy Code

    //header file with WordWrap as default value of false:
    class PACKAGE TYellowMemo : public TMemo
    {
    public:
        virtual __fastcall TYellowMemo(TComponent* Owner);
    __published:
        __property Color = {default=clYellow};
        __property WordWrap = {default=false};
    };

Specifying the default property value does not affect the workings of the component. You must still initialize the value in the component's constructor. Redeclaring the default ensures that Delphi knows when to write WordWrap to the form file.

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!