views:

317

answers:

8

Please could someone explain me what's the difference between public and published class members in Delphi?

I tried to look at Delphi help and I understand that these members have the same visibility, but I don't understand very well how they differ and when should I use published members instead of public ones.

Thanks a lot.

+6  A: 

The compiler generates RTTI (Run-Time Type Information) metadata for published members, but not for public members (by default). The main effect of this is that the published properties of an object will appear in the Object Inspector at design time.

I do not know if you are writing components, but if you do, you probably know that properties and events are normally published, so that they can be set using the Object Inspector.

Public

public
  property MyProperty: integer read FMyProperty write FMyProperty

MyProperty will not be visisble in the Object Inspector.

Published

published
  property MyProperty: integer read FMyProperty write FMyProperty

MyProperty will be visisble in the Object Inspector.

Andreas Rejbrand
While this is correct, the underlying cause for why it appears in the object inspector is that RTTI metadata is present for published members. In other words, what you're seeing is one effect of the underlying difference. A more correct answer is that published members have RTTI, while public members have not.
Lasse V. Karlsen
@Lasse V. Karlsen: Yes, you're right. But since this is probably the most visible difference (?), and no other answer have pointed it out yet, I will refrain from removing my answer right now.
Andreas Rejbrand
Sorry, I didn't make myself clear enough :) I agree, this is the most observable effect of the difference. My point was that you should point out that the underlying difference is RTTI, not remove your answer (even though other answers are pointing to RTTI now.)
Lasse V. Karlsen
I think its worth pointing out however that if you are asking a question regarding the difference between Public and Published methods then the term RTTI probably wont mean anything to you either. Andreas has provided a very useful answer.
Toby Allen
+4  A: 

Published properties will export Runtime Type Information (RTTI).

Have a look here about RTTI in Delphi

Ray
+3  A: 

Runtime Type Informations (RTTI) are only generated for published class members.

splash
Unless {$M+} / {$TYPEINFO ON} is specified, in this case public members will have RTTI too
mjustin
If that's true, @Mjustin, then it is a change from earlier Delphi versions. It used to be that *without* $M+, public and published were equivalent. That is, the published section did not generate RTTI unless $M+ was in effect.
Rob Kennedy
@Rob in Delphi 2009 I use {$M+} and RTTI with public properties. IIRC it also works with Delphi 6 (have to check)
mjustin
+6  A: 

Public properties and published properties have the same visibility, as you already stated. Published properties are included in RTTI, public properties aren't.

Roald van Doorn
RTTI stands for Run Time Type Information or the information that the Delphi Compiler needs at Design time to make the object inspector and other parts of the Delphi IDE work correctly at design time.
Toby Allen
For completeness: Delphi 2010 can generate RTTI for public members as well, in fact in can generate RTTI for all members, including protected and private. But published members are still the ones "published" by the Object Inspector and used by the automatic streaming mechanism.
Cosmin Prund
+1  A: 

At run-time, entries in the published and public sections are equally accessible.

The principal difference between them is that published items of a component appear in the Object Inspector at design-time.

This happens because, for fields in published section RTTI is automatically generated.

The Object Inspector picks this up and uses it to identify what to add to its list of properties and events.

Bharat
+1  A: 

As a side note, there is another special thing with published:

The default visibility of class members is published, so check for unsafe code like:

  TTopSecret = class(TObject)
    Name: string;
    Password: string;

    function DecryptPassword(const AValue): string;  
  public
    constructor Create(const AName, AEncryptedPassword: string);
  end; 

Name and Password are visible 'world-wide'.

mjustin
+2  A: 

In addition to the other answers:

Published properties are automatically stored by the streaming system.

For instance if you have a TComponent's descendant instance and write it to a TStream with WriteComponent, all (well, not all, but that is another question) published properties are written to the stream without any further coding.

Of course, the streaming system only can do that because the RTTI is available for those published properties.

Uwe Raabe
+2  A: 

It seems there are lots of good answers already, pointing out the Object INspector, RTTI, etc. These are all pieces of the puzzle.

If you take away the published keyword, the entire Delphi RAD tool design would require some way to specify which properties are stored in a DFM, inspected in the component property inspector, and can be reloaded at runtime from a DFM when the form or data module is created.

This, in a word, is what Published is for. It is interesting to me that the designers of QT (originally TrollTech, now part of Nokia) had to emulate this level of RTTI for their C++ RAD library "QT".

Warren P