Articles   Members Online:
-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 assign all property values of one class to another instance of the same c 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
19-Oct-02
Category
Object Pascal
Language
Delphi 2.x
Views
223
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

How can I assign all property values (or if it’s not possible only published 
property values, or some of them) of one class (TComponent) to another instance of 
the same class? What I want to do is:

MyComponent1. {property1} := MyComponent2. {property1};
{...}
MyComponent2. {propertyN} := MyComponent2. {propertyN};

Is there a better and shorter way to do this? I tried this: MyComponent1 := 
MyComponent2; But it doesn’t work. Why not? Can I point to the second component ?

Answer:

Solve 1:

MyComponent2 and MyComponent1 are pointers to your components, and this kind of 
assigment leads to MyComponent1 pointing to MyComponent2. But it will not copy its 
property values.

A better way is to override the assign method of your control, do all property 
assignment there and call it when you need to copy component attributes. Here's 
example:

1   procedure TMyComponent.Assign(Source: TPersistent);
2   begin
3     if Source is TMyComponent then
4     begin
5       property1 := TMyComponent(Source).property1;
6       { ... }
7     end
8     else
9       inherited Assign(Source);
10  end;
11  
12  //To assign properties you'll need to set this line in the code:
13  
14  MyComponent1.Assign(MyComponent2);



Solve 2:

15  procedure EqualClassProperties(AClass1, AClass2: TObject);
16  var
17    PropList: PPropList;
18    ClassTypeInfo: PTypeInfo;
19    ClassTypeData: PTypeData;
20    i: integer;
21    NumProps: Integer;
22    APersistent: TPersistent;
23  begin
24    if AClass1.ClassInfo <> AClass2.ClassInfo then
25      exit;
26    ClassTypeInfo := AClass1.ClassInfo;
27    ClassTypeData := GetTypeData(ClassTypeInfo);
28    if ClassTypeData.PropCount <> 0 then
29    begin
30      GetMem(PropList, SizeOf(PPropInfo) * ClassTypeData.PropCount);
31      try
32        GetPropInfos(AClass1.ClassInfo, PropList);
33        for i := 0 to ClassTypeData.PropCount - 1 do
34          if not (PropList[i]^.PropType^.Kind = tkMethod) then
35            {if Class1,2 is TControl/TWinControl on same form, its names must be 
36  unique}
37            if PropList[i]^.Name <> 'Name' then
38              if (PropList[i]^.PropType^.Kind = tkClass) then
39              begin
40                APersistent := TPersistent(GetObjectProp(AClass1, PropList[i]^.Name,
41                  TPersistent));
42                if APersistent <> nil then
43                  APersistent.Assign(TPersistent(GetObjectProp(AClass2,
44                    PropList[i]^.Name, TPersistent)))
45              end
46              else
47                SetPropValue(AClass1, PropList[i]^.Name, GetPropValue(AClass2,
48                  PropList[i]^.Name));
49      finally
50        FreeMem(PropList, SizeOf(PPropInfo) * ClassTypeData.PropCount);
51      end;
52    end;
53  end;


Note that this code skips object properties inherited other than TPersistent.

			
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