![]() |
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:
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.
case AType of
0: TFirstClass.Create.LoadFromStream; 1: TAnotherClass.Create.LoadFromStream; // ........ end; Is possible map number to class type:
Delphi-Quellcode:
function GetType(AType: Int32): TBaseClass;
begin // ????????????????? end; and do
Delphi-Quellcode:
;
GetType(AType).Create.LoadFromStream
:?: |
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; |
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 02:08 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz