Articles   Members Online: 3
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to Handle lots of classes at runtime Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
21-Jun-04
Category
Algorithm
Language
Delphi 4.x
Views
193
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Max Kleiner

Classes which can't support TPersistent or TPersistentClass had to implement 
another technique to manage the creation of obejcts at runtime 

Answer:

If a class inherits from TPersistent we can use RegisterClass in several units so 
we can use the function GetClass to convert a class name to a class instance: 

1   TFinanceClass = class of TFinance;
2   
3   var
4     refClass: TFinanceClass;
5   
6     refClass := TFinanceClass(getClass(values['RegClass']));
7     dynObj := refClass.Create;
8     dynObj.getCharge(9.2);
9   
10    RegisterClass(TFinance);


If you can't inherit from TPersistent we can use a "Name=Value" pair of objects in 
a stringlist and loaded when a module is created or by user events. So you can 
determine which object should be created. 

11  function initObjinList;
12  var //refClass: TFinanceClass;
13    dynObj: TFinance;
14    myClsname: string[240];
15  begin
16    with TStringList.Create do
17    begin
18      clear;
19      values['RegClass'] := 'TRegularCharge2';
20      values['PrefClass'] := 'TPreferredCharge2';
21      values['TrialClass'] := 'TTrialCharge2';
22      myClsname := 'TrialClass';
23      if indexofname(myClsname) > -1 then
24      begin
25        {refClass:= TFinanceClass(getClass(values['RegClass']));
26        dynObj:= refClass.Create;
27        dynObj.getCharge(9.2); }
28        case IndexOfname(myClsname) of
29          0: dynObj := TRegularCharge2.create;
30          1: dynObj := TPreferredCharge2.create;
31          2: dynObj := TTrialCharge2.create;
32        end;
33        dynObj.getcharge(9.1);
34        dynObj.Free;
35        result := true;
36      end;
37      Free;
38    end;
39  end;


The value is the class type and the name simplifies the maintainability of the 
classes. 

syntax: 

40  mystringlist.values[name] := value //write
41  myname := mystringlist.values[name] //read


I made only one function to show the simplicity but registering classes in a 
StringList and choosen from a case of structure should be separated in differnt 
functions. 
All the classes inherit from TFinance and in comparison to RegisterClass we don't 
use a ClassReference. 
Descendants override the public or protected getCharge() methods to perform their 
actions. 

42   TFinance = class
43  public
44    function getCharge(const Balance: double): double; virtual;
45      abstract;
46  end;
47  
48  TRegularCharge2 = class(TFinance)
49  public
50    function getCharge(const Balance: double): double; override;
51  end;
52  
53  TPreferredCharge2 = class(TFinance)
54  public
55    function getCharge(const Balance: double): double; override;
56  end;
57  
58  TTrialCharge2 = class(TFinance)
59  public
60    function getCharge(const Balance: double): double; override;
61  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC