RAD Studio
ContentsIndex
PreviousUpNext
Overriding the Constructor

When a component is placed on a form at design time, or when an application constructs a component at runtime, the component's constructor sets the property values. When a component is loaded from a form file, the application sets any properties changed at design time.

Note: When you override a constructor, the new constructor must call the inherited constructor before doing anything else. For more information, see Overriding methods.
For this example, your new component needs to override the constructor inherited from TMemo to set the WordWrap property to False. To achieve this, add the constructor override to the forward declaration, then write the new constructor in the implementation part of the unit:

type
  TWrapMemo = class(TMemo)
  public                                              { constructors are always public }
    constructor Create(AOwner: TComponent); override; { this syntax is always the same }
  end;
.
.
.
constructor TWrapMemo.Create(AOwner: TComponent);     { this goes after implementation }
begin
  inherited Create(AOwner);                           { ALWAYS do this first! }
  WordWrap := False;                                  { set the new desired value }
end;

 

class PACKAGE TYellowMemo : public TMemo
{
public:
    virtual __fastcall TYellowMemo(TComponent* Owner); // the constructor declaration
__published:
    __property Color;
};
__fastcall TYellowMemo::TYellowMemo(TComponent* Owner)
  : TMemo(Owner)                                // the constructor implementation first...                                                  // ...calls the constructor for TMemo
{
  Color = clYellow;                            // colors the component yellow
}

Now you can install the new component on the Tool palette and add it to a form. Note that the WordWrap property is now initialized to False

If you change an initial property value, you should also designate that value as the default. If you fail to match the value set by the constructor to the specified default value, Delphi cannot store and restore the proper value.

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