前几天问了这个问题,大家说用Assign函数
可是当对象中包含其他对象的时候,用该函数,包含的对象就不能赋值了啊包含的对象是指那些被声明为一个属性的其他对象。
比如我要赋值一个对象A,对象B为对象A的一个对象
当我用A.Assign(*)的时候,B还是为空啊

解决方案 »

  1.   

    The Assign method copies all properties from a Series component to another.
    Only the common properties shared by both source and destination Series are copied.
      

  2.   

    另外写一个Assign,先调用原先的Assign,然后再调用域里面对象的Assign
      

  3.   

    TPersistent = class(TObject)
      private
        procedure AssignError(Source: TPersistent);
      protected
        procedure AssignTo(Dest: TPersistent); virtual;
        procedure DefineProperties(Filer: TFiler); virtual;
        function  GetOwner: TPersistent; dynamic;
      public
        destructor Destroy; override;
        procedure Assign(Source: TPersistent); virtual;
        function  GetNamePath: string; dynamic;
      end;是虚的
    To militant(小猪油) 
    假定类里面的对象的Assign是正确的,那么就OK了,如果这些对象的Assign也不能完全复制,那么原则上本来就应该重写Assign,不存在麻烦的问题。
    另外,TPersistent的Assign是怎么实现的阿??
    procedure TPersistent.Assign(Source: TPersistent);
    begin
      if Source <> nil then Source.AssignTo(Self) else AssignError(nil);
    end;procedure TPersistent.AssignTo(Dest: TPersistent);
    begin
      Dest.AssignError(Self);
    end;procedure TPersistent.AssignError(Source: TPersistent);
    var
      SourceName: string;
    begin
      if Source <> nil then
        SourceName := Source.ClassName
      else
        SourceName := 'nil';
      raise EConvertError.CreateResFmt(@SAssignError, [SourceName, ClassName]);
    end;完全看不出哪里在复制procedure TBitmap.Assign(Source: TPersistent);
    var
      DIB: TDIBSection;
    begin
      if (Source = nil) or (Source is TBitmap) then
      begin
        EnterCriticalSection(BitmapImageLock);
        try
          if Source <> nil then
          begin
            TBitmap(Source).FImage.Reference;
            FImage.Release;
            FImage := TBitmap(Source).FImage;
            FTransparent := TBitmap(Source).FTransparent;
            FTransparentColor := TBitmap(Source).FTransparentColor;
            FTransparentMode := TBitmap(Source).FTransparentMode;
          end
          else
          begin
            FillChar(DIB, Sizeof(DIB), 0);
            NewImage(0, 0, DIB, False);
          end;
        finally
          LeaveCriticalSection(BitmapImageLock);
        end;
        PaletteModified := Palette <> 0;
        Changed(Self);
      end
      else inherited Assign(Source);
    end;
      

  4.   

    从TBitmap的Assign看已经有复制的步骤了,而TPersitent的assign形同虚设,只会显示'Cannot assign a .. to a ..',所以要用Assign从TPersistent直接继承是不行的,要自己动手写。
    我也不知道通用的Assign最开始在哪个类里面实现的,TBitmap的这个assign好像是从TPersistent继承以来头次实现,所以。。要一劳永逸的复制全部元素应该从哪个类继承呢??