RAD Studio |  |

Overriding the DefineProperties Method

Once you have created methods to store and load your property value, you can override the component's DefineProperties method. Delphi calls this method when it loads or stores the component. In the DefineProperties method, you must call the DefineProperty method or the DefineBinaryProperty method of the current filer, passing it the method to use for loading or saving your property value. If your load and store methods are of type TWriterProc and type TReaderProc, then you call the filer's DefineProperty method. If you created methods of type TStreamProc, call DefineBinaryProperty instead.

No matter which method you use to define the property, you pass it the methods that store and load your property value as well as a boolean value indicating whether the property value needs to be written. If the value can be inherited or has a default value, you do not need to write it.

For example, given the LoadCompProperty method of type TReaderProc and the StoreCompProperty method of type TWriterProc, you would override DefineProperties as follows:

Copy Code

    procedure TSampleComponent.DefineProperties(Filer: TFiler);
      function DoWrite: Boolean;
      begin
        if Filer.Ancestor <> nil then { check Ancestor for an inherited value }
        begin
          if TSampleComponent(Filer.Ancestor).MyCompProperty = nil then
            Result := MyCompProperty <> nil
          else   if (MyCompProperty = nil) or
             (TMy5Comp(Filer.Ancestor).MyCompProperty.Name <> MyCompProperty.Name) then
            Result := True
          else Result := False;
        end
        else { no inherited value -- check for default (nil) value }
          Result := MyCompProperty <> nil;
      end;
    begin
      inherited; { allow base classes to define properties }
      Filer.DefineProperty('MyCompProperty', LoadCompProperty, StoreCompProperty, DoWrite);
    end;

Copy Code

    void __fastcall TSampleComponent::DefineProperties(TFiler *Filer)
    {
      // before we do anything, let the base class define its properties.
      // Note that this example assumes that TSampleComponent derives directly from TComponent
      TComponent::DefineProperties(Filer);
      bool WriteValue;
      if (Filer->Ancestor) // check for inherited value
      {
        if ((TSampleComponent *)Filer->Ancestor)->MyCompProperty == NULL)
          WriteValue = (MyCompProperty != NULL);
        else if ((MyCompProperty == NULL) ||
                 (((TSampleComponent *)Filer->Ancestor)->MyCompProperty->Name !=
                                                         MyCompProperty->Name))
          WriteValue = true;
        else WriteValue = false;
      }
      else // no inherited value, write property if not null
        WriteValue = (MyCompProperty != NULL);
      Filer->DefineProperty("MyCompProperty ",LoadCompProperty,StoreCompProperty, WriteValue);
    end;

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