部分源代码如下:
unit Unit1;
.....
....
uses
   GetDataModule;procedure TForm1.Button1Click(Sender: TObject);
var
  FModule: TDataModule;
  FOperation: TOperation;
begin
  FModule:= TDataModule.Create(Self);
  FOperation := TOperation.Create(nil);
  FOperation.Channels := 88;
  FModule.Operations.Add.Channels :=12;
  FModule.Operations[0] := FOperation;
  FMOdule.Destroy;
end;unit GetDataModule;
....
....
TOperation = class (TComponent)
   private
     FChannels:integer;
     ...
   public
     property Channels: Integer read GetChannels write SetChannels;
     constructor Create(AOwner:TOperations);
end;  TOperations = class (TObject)
  private
    FList: TList;
    FOwner: TDataModule;
    procedure SetOperations(Index: Integer; const Value: TOperation);
    function GetOperations(Index: Integer): TOperation;
  public
    constructor Create(AOwner:TDataModule);
    function Add: TOperation;
    property Operations[Index: Integer]: TOperation read GetOperations Write SetOperations; default;
  end;TDataModule = class (TObject)
   private
     FOperations: TOperations;
   .. 
   ..
   public
   ...
    constructor Create(AOwner:TComponent);
    Property Operations: TOperations read FOperations;
end;constructor TOperations.Create(AOwner:TDataModule);
begin
  FList := TList.Create;
end;
function TOperations.Add: TOperation;
var
  FOperation: TOperation;
begin
    FOperation := TOperation.Create(Self);
    FList.Add(FOperation);
    Result := TOperation(FList.Items[FList.Count -1]);
end;function TOperations.GetOperations(Index: Integer): TOperation;
begin
  Result := (FList.Items[Index]);
end;procedure TOperations.SetOperations(Index: Integer;
  const Value: TOperation);
begin
  TOperation(FList[Index]).Assign(Value);//ERROR LINE
end;为什么当程序运行到FModule.Operations[0] := FOperation;的时候通过调试发现错误发生在ERROR LINE这里,错误信息为:Cannot Assign A TOperation To A TOperation,请问为什么会出现这个错误?如何解决?还有TA = class of TB代表什么意思?请各位帮帮忙,如果分不够可以另外开帖

解决方案 »

  1.   

    >>TOperations = class (TObject)
    修改為
     TOperations = class (TControl)
    試下
      

  2.   

    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.TOperation = class (TComponent) ,but
    TOperations = class (TObject);2.A class-reference type, sometimes called a metaclass, is denoted by a construction of the formclass of typewhere type is any class type. The identifier type itself denotes a value whose type is class of type. If type1 is an ancestor of type2, then class of type2 is assignment-compatible with class of type1. Thustype TClass = class of TObject;
    var AnyObj: TClass;declares a variable called AnyObj that can hold a reference to any class. (The definition of a class-reference type cannot occur directly in a variable declaration or parameter list.) You can assign the value nil to a variable of any class-reference type.To see how class-reference types are used, look at the declaration of the constructor for TCollection (in the Classes unit):type TCollectionItemClass = class of TCollectionItem;
      ...
    constructor Create(ItemClass: TCollectionItemClass);This declaration says that to create a TCollection instance object, you must pass to the constructor the name of a class descending from TCollectionItem.Class-reference types are useful when you want to invoke a class method or virtual constructor on a class or object whose actual type is unknown at compile time.TA = class of TB ,
    表示TA是继承TB的类;
      

  3.   

    TA = class of TB ,
    表示TA是一个类TB的类型,而不是继承
      

  4.   

    procedure TOperations.SetOperations(Index: Integer;
      const Value: TOperation);
    begin
      FList[Index]:=Value
    end;
      

  5.   

    楼上的兄弟谢谢了,问题是解决了,可是为什么会这样呢?
    还有TA= class of TB到底是什么意思?它和类的继承有什么区别呢?
      

  6.   

    TA = class of TB ,
    表示TA是一个类TB的类型,