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 Add new methods and properties without registering new component 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
22-May-03
Category
Algorithm
Language
Delphi 3.x
Views
120
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Ernesto De Spirito

Is there a way to add new methods and properties to a component without having its 
soruce code and having to install a descendant component?

Answer:

Adding new methods and properties 

Sometimes we need to add new methods and properties to an existing component (or 
change the visibility of existing properties). One way of doing this is modifiying 
the component, but this implies having to recompile its package and we would have 
to redistribute our changes if we wanted our application to be compiled by others, 
and that would 
be a bother for the recipients. Sometimes we may not even have that choice because 
we may not have the source code. In these situations, better would be to subclass 
(derive) the component and add new properties and methods. For example: 

1   type
2     TEditX = class(TEdit)
3     public
4       function GetForeColor: TColor;
5       procedure SetForeColor(color: TColor);
6       property ForeColor: TColor read GetForeColor write SetForeColor;
7     end;
8   
9   //These methods could for example be implemented this way: 
10  
11  function TEditX.GetForeColor: TColor;
12  begin
13    Result := Font.Color;
14  end;
15  
16  procedure TEditX.SetForeColor(color: TColor);
17  begin
18    Font.Color := Color;
19  end;


It's a silly example, of course, but it serves the purpose. 

Casting to the new class 

We don't need to intall this new component and register it in the components 
palette or replace existing controls in our applications (which would be an 
unpayable penalty for such small changes and/or additions). Instead, any time we 
want to access the new properties and methods, we can just cast the object (for 
example Edit1) to our new class. For example: 

20  TEditX(Edit1).ForeColor := clRed;
21  
22  //or 
23  
24  TEditX(Edit1).SetForeColor(clRed);


Warning: This casting to a descendant class can only be done if the new class adds 
new properties and static methods, but without adding new fields and new virtual or 
dynamic methods, although in theory you can override existing virtual methods. 
Also, the visibility of existing properties can be changed, as in the InplaceEditor 
example 
explained in the article "Accessing hidden properties". 

Copyright (c) 2001 Ernesto De Spiritomailto:edspirito@latiumsoftware.com
Visit: http://www.latiumsoftware.com/delphi-newsletter.phphttp://www.latiumsoftware.com/delphi-newsletter.php

			
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