?
Solved

Retaining Design Time Info in a Component at Run Time

Posted on 2005-05-01
10
Medium Priority
?
213 Views
Last Modified: 2013-11-23
Greetings,

I have a component which is descended from TCustomPanel.  The component has properties which are of a type descended from TPersistent.
I have to instantiate these properties on creation of the main component.  How can I retain the properties of these objects at run time?  They have published properties and are editable in the object inspector by virtue of being a TClassProperty.  The main components properties are retained ok at run time but since the class properties have to be instantiated again, their information is lost.  Is there any way to retrieve this at run time?
0
Comment
Question by:LordWolfy
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13907423
I would have thought that only published properties in the object inspector would be saved. Maybe you would need to store these other things you want outside of the application like the registry or an ini file.
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13907445
Or if they will never change, just store them as constants in a unit in your application and load them up at runtime
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 13907464
i would guess, since your subClasses are already inherited from TPersistent

published
  property subclass : TSubClass read FSubClass;
  ...

should be enough

just from head, could be also a mistake

meikl ;-)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:LordWolfy
ID: 13911430
Greetings,

Kretschmar - thats exactly as I have it but to illustrate the point here is a more common example :-

Suppose you had a component with a TFont property.  You would have to instantiate that TFont in your constructor.  The problem is, this
would also have to be done at run time.  The newly created TFont has no way of knowing what information was given at design time, yet there are plenty of delphi components which obviously can retain this information.  I imagine RTTI is used in some way but I cant figure out how.
0
 
LVL 2

Author Comment

by:LordWolfy
ID: 13913010

Once again I have found the answer on my own.  In order to retain design time information at run time in a TPersistent class property you must :-

1. Override the Assign method of the class so that it knows how to deal with other instances of its type.

2. Make sure that the parent object can write to this property via a set procedure.

3. Call Assign from the set procedure.

Example :-


Unit SomeUnit;


Interface

TSomeClassType = Class (TPersistent)
Private
     (Internal stuff....)
     .
     .

Public
     (Public Methods and properties.....)
     .
     .

Published
     (Published Methods and properties.....)
     .
     .
      Procedure Assign (Source : TPersistent); Override;
End;


TMainObject = Class (TComponent)
Private
     (Internal stuff......)
     .
     .
     ISomeClassProperty : TSomeClassType;

Protected
     Procedure ISetSomeClassProperty (ASomeClassProperty : TSomeClassType); Virtual;

Public
     (Public methods and properties.....)
     .
     .

Published
     (Published Methods and properties.....)
     .
     .
     Property SomeClassProperty : TSomeClassType Read ISomeClassProperty Write ISetSomeClassProperty;
End;


Implementation

(Methods for TSomeClassType.....)
.
.

Procedure TSomeClassType.Assign (Source : TPersistent);
Var
   SomeClassVar : TSomeClassType;
Begin
     If Source Is TSomeClassType Then
     Begin
          SomeClassVar := TSomeClassType (Source);
          (Assignment stuff.....)
          .
          .
     End
     Else
         Inherited Assign (Source);      
End;


(Methods for TMainObject.....)
.
.

Procedure TMainObject.ISetSomeClassProperty (ASomeClassProperty : TSomeClassType);
Begin
     ISomeClassProperty.Assign (ASomeClassProperty);
End;
0
 
LVL 2

Author Comment

by:LordWolfy
ID: 13913030

Oops - The assign method of TSomeClassType should be in public not published
0
 
LVL 12

Expert Comment

by:andrewjb
ID: 13916084
PAQ it. Useful information when someone else has the same problem and searches...

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 13916149
i recommend also paq and refund
0
 
LVL 1

Accepted Solution

by:
GhostMod earned 0 total points
ID: 13952281
PAQd, 200 points refunded.

GhostMod
Community Support Moderator
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Integration Management Part 2
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question