0

Please take a look to the bellow code part of a 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;

When the method SetField from TSqlField is processed I would like to access the property Name from TSqlField : TCollectionItem so I can rename it.

Remark: The owner of TCollection could be one or another TComponent.

Please some suggestions about how can this be done.

REALSOFO
  • 852
  • 9
  • 37
  • 2
    Typecast `GetOwner as TMyField`. – Victoria Sep 28 '17 at 12:29
  • @Victoria: that will not work in the code shown. `TSqlField` derives directly from `TPersistent` and does not override the virtual `GetOwner()` method. `TPersistent.GetOwner()` returns nil by default. In its current form, `TSqlField` has no linkage to the `TMyField` that owns it. `TMyField` needs to pass its `Self` pointer to `TSqlField` so `TSqlField` can store it in a private member and return it from an overridden `GetOwner()`. – Remy Lebeau Sep 28 '17 at 15:45
  • @Remy, now I see. I must have been full of collections here. I understand how it should work, but thanks :) – Victoria Sep 28 '17 at 15:48

0 Answers0