Delphi Clinic C++Builder Gate Training & Consultancy Delphi Notes Weblog Dr.Bob's Webshop
Bob Swart (aka Dr.Bob) - Medical Officer Dr.Bob's Kylix Kicks
 Dr.Bob's Tip-Of-The-Hat #14
See Also: Delphi Papers, Columns and other Tip-Of-The-Hats

Published not allowed?
For this Tip-Of-The-Hat (especially applicable to Delphi 1), let's take a look at the following class definition in which I want to publish a certain field called Value:

  unit TipHat01;
  interface
  type
    TError200 = class
      private
        FValue: Integer;
      published
        property Value: Integer read FValue write FValue;
    end;

  implementation

  end.
Using Delphi version 2 or higher (or C++Builder), there is no problem compiling this code. However, when we try to compile it using Delphi 1.x, we get a compiler error 200, "PUBLISHED not allowed in this class". Unfortunately, there's no help available in Delphi 1.x for error 200 (an F1 just after the error gives "topic not found", and a manual search only finds compiler errors documented up to 198.

What are the rules for using "published"? You are getting the compiler error because a class cannot have published parts unless the class is compiled with the {$M+} directive set or it descends from a class that was compiled with this setting. Published properties are supported through Run-time type information. If the {$M+} directive is not specified then no RTTI exists for the class. So, in the listing above, you need to add {$M+} to the top of the unit to make it compile without an error message.
For some reason, Delphi 2 and higher no longer display the compiler error message, although officially, {$M+} is introduced at the TPersistent level, and not for a generic class type (so we should also define the {$M+} directive here).

Epilogue
Identifiers in the PRIVATE section of a class or object are of course private, and identifiers in the PUBLIC section are public. But what about the component declarations added by the IDE in the top of the class declaration above the PRIVATE section? What access rights do they have?
The answer is clear: unless explicitly specified otherwise, all fields, methods and properties of objects are published: they're both public and automatically appear in the Object inspector. Published parts have run-time type info generated for them which is available to the object inspector. You can find more info on this in the Component Writers Guide that comes with your version of Delphi.


This webpage © 2001-2009 by Bob Swart (aka Dr.Bob - www.drbob42.com). All Rights Reserved