Initializing the Data Link

From RAD Studio
Jump to: navigation, search

Go Up to Adding the Data Link


A data-aware control needs access to its data link throughout its existence, so it must construct the data link object as part of its own constructor, and destroy the data link object before it is itself destroyed.

Override the Create and Destroy methods of the calendar to construct and destroy the datalink object, respectively:

type
  TDBCalendar = class(TSampleCalendar)
  public                                  { constructors and destructors are always public }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    .
    .
    .
  end;
.
.
.
constructor TDBCalendar.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);                  { always call the inherited constructor first }
  FDataLink := TFieldDataLink.Create;                     { construct the datalink object }
  FDataLink.Control := self;                {let the datalink know about the calendar }
  FReadOnly := True;                                                { this is already here }
end;
destructor TDBCalendar.Destroy;
begin
  FDataLink.Free;                                  { always destroy owned objects first... }
  inherited Destroy;                                   { ...then call inherited destructor }
end;
class PACKAGE TDBCalendar : public TSampleCalendar
{
public:
    virtual __fastcall TDBCalendar(TComponent *Owner);
    __fastcall ~TDBCalendar();
};
__fastcall TDBCalendar::TDBCalendar(TComponent* Owner) : TSampleCalendar(Owner)
{
    FReadOnly = true;
    FDataLink = new TFieldDataLink();
    FDataLink->Control = this;
}
__fastcall TDBCalendar::~TDBCalendar()
{
    FDataLink->Control = NULL;
    FDataLink->OnUpdateData = NULL;
    delete FDataLink;
}

Now you have a complete data link, but you have not yet told the control what data it should read from the linked field. The next section explains how to do that.

See Also