.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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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 / 14
    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

    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




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