AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Sprachen und Entwicklungsumgebungen Object-Pascal / Delphi-Language Delphi Stored classes identified by integer - how to get type base on number?

Stored classes identified by integer - how to get type base on number?

Ein Thema von WojTec · begonnen am 28. Nov 2011 · letzter Beitrag vom 28. Nov 2011
Antwort Antwort
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#1

Stored classes identified by integer - how to get type base on number?

  Alt 28. Nov 2011, 16:54
Delphi-Version: 2010
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

GetType(AType).Create.LoadFromStream ;

  Mit Zitat antworten Zitat
Benutzerbild von Aphton
Aphton

Registriert seit: 31. Mai 2009
1.198 Beiträge
 
Turbo Delphi für Win32
 
#2

AW: Stored classes identified by integer - how to get type base on number?

  Alt 28. Nov 2011, 17:02
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;
das Erkennen beginnt, wenn der Erkennende vom zu Erkennenden Abstand nimmt
MfG

Geändert von Aphton (28. Nov 2011 um 18:06 Uhr)
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#3

Re: Stored classes identified by integer - how to get type base on number?

  Alt 28. Nov 2011, 19:05
Thanks. I also tried with metaclasses and for now is working, let's see what in more complicated structure
  Mit Zitat antworten Zitat
Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 02:53 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