Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Stored classes identified by integer - how to get type base on number? (https://www.delphipraxis.net/164752-stored-classes-identified-integer-how-get-type-base-number.html)

WojTec 28. Nov 2011 16:54

Delphi-Version: 2010

Stored classes identified by integer - how to get type base on number?
 
I'm storing in stream classes fields values (each class has SaveToStream/LoadFrom strem methods). Now for example if I need load from file these classes to object I'm doing:

Delphi-Quellcode:
case AType of
  0: TFirstClass.Create.LoadFromStream;
  1: TAnotherClass.Create.LoadFromStream;
  // ........
end;
This was good (but yes - lame) when was small set of classes. Now I have many ones and this method is also annoying when need to update.

Is possible map number to class type:

Delphi-Quellcode:
function GetType(AType: Int32): TBaseClass;
begin
  // ?????????????????
end;


and do

Delphi-Quellcode:
GetType(AType).Create.LoadFromStream
;

:?:

Aphton 28. Nov 2011 17:02

AW: Stored classes identified by integer - how to get type base on number?
 
You could probably try this out (depending on your Delphi Version)

Delphi-Quellcode:
type
  TClassEnum = (ceFirstClass, ceAnotherClass{, ...}); // ...

  TBaseClass = class of TBase; // TBase is your base class (TFirstClass and TAnotherClass inherit from TBase!)

const
  cClassEnumTypes: Array[TClassEnum] of TBaseClass = (TFirstClass, TAnotherClass{, ...});

// AType: TClassEnum;
var
  Instance: TBase;
begin
  Instance := cClassEnum[AType].Create;
  Instance.LoadFromStream;
  {...}
end;

WojTec 28. Nov 2011 19:05

Re: Stored classes identified by integer - how to get type base on number?
 
Thanks. I also tried with metaclasses and for now is working, let's see what in more complicated structure :)


Alle Zeitangaben in WEZ +1. Es ist jetzt 04:48 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz