.NET
  Home arrow .NET arrow Page 10 - The Delphi Language, Part 2
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

The Delphi Language, Part 2
By: Xavier Pacheco
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 15
    2004-07-13

    Table of Contents:
  • The Delphi Language, Part 2
  • Out Parameters, Constant Parameters, and Open Array Parameters
  • Scope and Units and Namespaces
  • The uses Clause and Circular Unit References
  • Object-Oriented Programming
  • Using Delphi Objects
  • Methods
  • Class References and Properties
  • Events
  • Visibility Specifiers, Friend Classes and Class Helpers
  • Nested Types, Operator Overloading and Attributes
  • Interfaces
  • Using Interfaces, SEH
  • Exception Classes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    The Delphi Language, Part 2 - Visibility Specifiers, Friend Classes and Class Helpers


    (Page 10 of 14 )

    Visibility Specifiers

    The Delphi language offers you further control over the behavior of your objects by enabling you to declare fields and methods with directives such as private, strict private, protected, strict protected, public, and published. The syntax for using these directives is as follows:

    TSomeObject = class
    private
      APrivateVariable: Integer;
      AnotherPrivateVariable: Boolean;
    strict private
      procedure AStrictPrivateMethod;
    protected
      procedure AProtectedProcedure;
      function ProtectMe: Byte;
    strict protected
      procedure AStrictProtectedMethod;
    public
      constructor APublicContructor;
      destructor Destroy; override; // a public destructor
    published 
      property AProperty: Integer
      read APrivateVariable write APrivateVariable;
    end;

    You can place as many fields or methods as you want under each directive. Style dictates that you should indent the specifier the same as you indent the classname. The meanings of these directives follow:

    • private—These parts of your object are accessible only to code in the same unit as your object's implementation. Use this directive to hide implementation details of your objects from users and to prevent users from directly modifying sensitive members of your object.

    • strict private—Members are accessible within the declaring class and not within the same unit. Used for more strict data encapsulation than private.

    • protected—Your object's protected members can be accessed by descendants of your object. This capability enables you to hide the implementation details of your object from users while still providing maximum flexibility to descendants of your object.

    • strict protected—Members are accessible only within the declaring class and ancestors and not within declaring units. Used for more strict data encapsulation than protected.

    • public—These fields and methods are accessible anywhere in your program. Object constructors and destructors always should be public.

    • published—Identical to public from a visibility standpoint. Published has the additional benefit of adding the [Browsable(true)] attribute to properties contained within, which causes them to be displayed in the Object Inspector when used in the form designer. Attributes are discussed later in this chapter.


    Note - The meaning of published represents a fairly substantial departure from the Win32 implementation of the Delphi language. In Win32, Runtime Type Information (RTTI) was generated for published properties. The .NET equivalent of RTTI is Reflection; however, Reflection is possible on all class elements, regardless of visibility specifier.


    Here, then, is code for the TMyObject class that was introduced earlier, with directives added to improve the integrity of the object:

    TMyObject = class
    private
      SomeValue: Integer;
      procedure SetSomeValue(AValue: Integer);
    published
      property Value: Integer read SomeValue write SetSomeValue;
    end;

    procedure TMyObject.SetSomeValue(AValue: Integer);
    begin
      if SomeValue <> AValue then
        SomeValue := AValue;
    end;

    Now, users of your object will not be able to modify the value of SomeValue directly, and they will have to go through the interface provided by the property Value to modify the object's data.

    "Friend" Classes

    The C++ language has a concept of friend classes (that is, classes that are allowed access to the private data and functions in other classes). This is accomplished in C++ using the friend keyword. .NET languages, such as Delphi and C#, have a similar concept, although the implementation is different. Non- strict, private, and protected members of a class are visible and accessible to other classes and code declared within the same unit namespace.

    Class Helpers

    Class helpers provide a means to extend a class without modifying the actual class. Instead, a new class, the helper, is created and its methods effectively bolted on to the original class. This enables users of the original class to call methods of the helper class as if they were methods of the original class.

    The following code example creates a simple class and class helper and demonstrates calling a method on the helper class:

    program Helpers;

    {$APPTYPE CONSOLE}

    type
      TFoo = class
        procedure AProc;
      end;

      TFooHelper = class helper for TFoo
        procedure AHelperProc;
      end;

    { TFoo }

    procedure TFoo.AProc; 
    begin 
      WriteLn('TFoo.AProc'); 
    end;

    { TFooHelper }

    procedure TFooHelper.AHelperProc;
    begin
      WriteLn('TFooHelper.AHelperProc');
      AProc;
    end;

    var
      Foo: TFoo;
    begin
      Foo := TFoo.Create;
      Foo.AHelperProc;
    end.


    Caution - Class helpers are an interesting feature, but not a feature that generally lends itself to good software design. This feature is in the language mostly because Borland needed it to hide the differences between standard .NET classes and similar classes in Win32 Delphi. The occasion should be rare when a good design involves the use of class helpers.


    This chapter is from Delphi for .NET Developer's Guide, by Xavier Pacheco (Sams, 2004, ISBN: 0-672-32443-1). Check it out at your favorite bookstore today.

    Buy this book now.

    More .NET Articles
    More By Xavier Pacheco


     

    .NET ARTICLES

    - The Transformed XML Explorer in MFC
    - List Control and Property Grid with the MFC ...
    - Font, Shell and Masked Edit Controls for MFC
    - Color, Link and Image Editor Controls for M...
    - New Controls for MFC
    - The Windows Ribbon Framework
    - Markup Language for the Ribbon Framework
    - Visually Upgrade Your MFC Project
    - New Features for the Statusbar in MFC
    - Working with the Statusbar in MFC
    - Iron Speed Design v60 Review
    - Binary and XML Serialization
    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT