Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Published fields or methods? 1

Status
Not open for further replies.

Farahani

Programmer
May 5, 2002
9
0
0
IR
Hi,

Can anybody tell me the benefits of a published field or method ?
 
Here is some info from Marco Cantu's Mastering Delphi 6 book:

" The published Keyword
Along with the
Code:
 public, protected
and
Code:
 private
access directives, you can use a fourth one, called
Code:
 published
. For any
Code:
 published
field, property, or method, the compiler generates extended RTTI [Run-Time Type Information] information, so that Delphi's run time environment or a program can query a class for its published interface. For example, every Delphi component has a published interface that is used by the IDE [Integrated Development Environment], in particular the Object Inspector. A regular use of
Code:
 published
fields is important when you write components. Usually, the
Code:
 published
part of a component contains no fields or methods but properties and events.

When Delphi generates a form or data module, it places the definitions of its components and methods (the event handlers) in the first portion of its definition, before the
Code:
 public
and
Code:
 private
keywords. These fields and methods of the initial portion of the class are
Code:
 published
. The default is
Code:
 published
when no special keyword is added before an element of a component class."

Hope this helps!
Clive [infinity]
 
A published field can let you allow access to Fields without letting the end user edit the field itself.

For example

Private
FDate : TDateTime
function GetDate:TDateTime;
function SetDate(ADate:TDateTime):boolean;
published
property Date:TDateTime read GetDate write SetDate;

This is a fake example, but you can see that it lets you edit and read a property via functions (which may include necessary validations) rather than letting the user directly edit a field value. The user has no real access to FDate, but must access it through the read write functions of the published property.


 
Actually, you can have properties that are not published as well. It is properties that allow you to use get/set functions.

As Stretchwickster mentioned, being in the published gives the property RTTI info which allows it to show up in the object inspector. It also allows you to query at runtime for whether that property exists. For example using RTTI and the published property ReadOnly that exists for most components you could write a generic function like below to set the readonly property if it exists.


uses typinfo;
procedure SetReadOnly(someComponent: TComponent);
var propInfo: pPropInfo;
begin
{Get read only property if it exists}
propInfo := GetPropInfo(someComponent.ClassInfo, 'ReadOnly')
if assigned(propInfo) then
{Set read only to true}
SetEnumProp(someComponent, propInfo, 'True');
end;
TealWren
 
Hi again!

Thanks for your answers, but these don't solve my problem!
I am familiar with published keyword and component writing, but I can't understand published fields & methods.

unlike published proprties & events, published field & methods don't appear in Object Inspector (? - I didn't test this) and GetPropInfo doesn't apply to them.

Bye!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top