TCollectionItem中的TPersistent组件/从TPersistent

时间:2017-09-28 12:10:35

标签: delphi tpersistent tcollectionitem

请查看TComponent

的下方代码部分
TMyField = class(TCollectionItem)
private
  FName: String
  FSqlField: TSqlField;
  procedure SetName(const Value: String);
  procedure SetSqlField(const Value: TSqlField)
published
  property Name: String read FName write SetName;
  property SqlField: TSqlField read FSqlField write SetSqlField;
end;  

TSqlField = class(TPersistent)
  private
    FAlias: String;
    FName: String;
    FTable: String;
    procedure SetField(Index: Integer; Value: String);
  public
    procedure Assign(Source: TPersistent); override;
  published
    property Alias: String index 0 read FAlias write SetField;
    property Name: String index 1 read FName write SetField;
    property Table: String index 2 read FTable write SetField;
  end;

procedure TSqlField.SetField(Index: Integer; Value: String);
var
  FOwner: TMyField;
begin
  if (Value <> FAlias) or (Value <> FName) or (Value <> FTable) then
    begin
      case Index of
        0: if Value <> FAlias then FAlias:= Value;
        1: if Value <> FName then FName:= Value;
        2: if Value <> FTable then FTable:= Value;
      end;

      if FName <> '' then
        begin
          FOwner:= GetOwner; //Error here: E2010 Incompatible types: 'TMyField' and 'TPersistent'
          if FTable <> '' then FOwner.Name:= Format('%s_%s',[FTable,FName])
          else FOwner.Name:= FName;
        end;
    end;
end;

处理SetField中的TSqlField方法时,我想从Name访问该属性TSqlField : TCollectionItem,以便重命名。

备注:TCollection的所有者可以是一个或另一个TComponent

请提供一些关于如何做到的建议。

0 个答案:

没有答案