Help! need your advice.

Help! need your advice.
I want to declare a genric class refrence type as like this, but XE3 could not. 

THashClass = class of THash;
THash = class;

The error is the following;
[dcc32 Error] gHash.pas(31): E2029 ';' expected but '<' found

How to solve the problem ?

Comments

  1. You cannot have a class reference to a generic class.

    Why do you need it anyway?

    ReplyDelete
  2. Stefan Glienke Sorry for my english. I'd like to have a class function which returns itself. Like this...

    type
      TBobJenkinsClass = class of TBobJenkins;
      TBobJenkins = class
      private
        class var
          FHashValue: Integer;
      public
        class function Hash(const ASrc: TStream; InitData: Integer = 0): TBobJenkinsClass; overload;
        class function Hash(const AValue: String; InitData: Integer = 0): TBobJenkinsClass; overload;
        class function HashFromFile(const AFileName: String; InitData: Integer = 0): TBobJenkinsClass;
        class function AsString: String;

        class property AsInteger: Integer read FHashValue;
      end;

    ReplyDelete
  3. Stefan Glienke Oh. Thank you for your answer :)

    ReplyDelete
  4. I guess the only possibility would be to let the generic class inherit from a non generic one on which you can declare a class reference.

    ReplyDelete
  5. Michael Jung Why not this:
    program Project1;
    {$APPTYPE CONSOLE}
    {$R *.res}
    uses
      System.SysUtils;
    type
      TBobJenkinsRoot = class
      end;
      TBobJenkinsClass = class of TBobJenkinsRoot;
      TBobJenkins = class(TBobJenkinsRoot)
      private
        class var
          FHashValue: Integer;
      public
        class function Hash(const AValue: String; InitData: Integer = 0): TBobJenkinsClass; overload;
        class property AsInteger: Integer read FHashValue;
      end;
    { TBobJenkins }
    class function TBobJenkins.Hash(const AValue: String;
      InitData: Integer): TBobJenkinsClass;
    begin
        Result := TBobJenkins;
    end;
    var
       Cl : TBobJenkinsClass;
    begin
      try
        Cl := TBobJenkins.Hash('123');
      except
        on E: Exception do
          Writeln(E.ClassName, ': ', E.Message);
      end;
    end.

    ReplyDelete
  6. François Piette That is what I meant, however there might be a problem with accessing the class var because these are not virtual and actually some members have to be moved to the base class because otherwise there is nothing to do with your metaclass.

    ReplyDelete

Post a Comment