.NET
  Home arrow .NET arrow Page 2 - 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 - Out Parameters, Constant Parameters, and Open Array Parameters


    (Page 2 of 14 )

     

    Like var parameters, out parameters provide a means for a routine to return a value to the caller in the form of a parameter. However, although var parameters must be initialized to a valid value prior to calling the routine, out parameters make no assumption about the validity of the incoming parameter. For reference types, this means that the reference will be completely discarded on entering the routine.

    procedure ReturnMe(out O: TObject);
    begin
      O := SomeObject.Create;
    end;

    Here's a rule of thumb: Use var for in/out parameters and out for out only parameters.

    Constant Parameters

    If you don't want the value of a parameter passed into a function to change, you can declare it with the const keyword. Here's an example of a procedure declaration that receives a constant string parameter:

    procedure Goon(const s: string);

    Open Array Parameters

    Open array parameters provide you with the capability for passing a variable number of arguments to functions and procedures. You can either declare open arrays of some homogenous type or const arrays of differing types. The following code declares a function that accepts an open array of integers:

    function AddEmUp(A: array of Integer): Integer;

    You can pass to open array routines variables, constants, or expressions of any array type (dynamic, static, or incoming open). The following code demonstrates this by calling AddEmUp() and passing a variety of different elements:

    var
      I, Rez: Integer;
    const
      J = 23;
    begin
      I := 8;
      Rez := AddEmUp([I, I + 50, J, 89]);

    You can also directly pass an array to an open array routine as shown here:

    var 
      A: array of integer;
    begin
      SetLength(A, 10);
      for i := Low(A) to High(A) do
        A[i] := i; 
      Rez := AddEmUpConst(A);

    In order to work with an open array inside the function or procedure, you can use the High(), Low(), and Length() functions to obtain information about the array. To illustrate this, the following code shows an implementation of the AddEmUp() function that returns the sum of all the numbers passed in A:

    function AddEmUp(A: array of Integer): Integer;
    var
      i: Integer;
    begin
      Result := 0;
      for i := Low(A) to High(A) do
        inc(Result, A[i]);
    end;

    The Delphi language also supports an array of const, which allows you to pass heterogeneous data types in an array to a routine. The syntax for defining a routine that accepts an array of const is as follows:

    procedure WhatHaveIGot(A: array of const);

    You could call the preceding procedure with the following syntax:

    WhatHaveIGot(['Tabasco', 90, 5.6, 3.14159, True, 's']);

    The compiler passes each element of the array as a System.Object, so it can easily be handled as such in the receiving function. As an example of how to work with array of const, the following implementation for WhatHaveIGot() iterates through the array and shows a message to the user indicating what type of data was passed in which index:

    procedure WhatHaveIGot(A: array of const);
    var
      i: Integer;
    begin
      for i := Low(A) to High(A) do
        WriteLn('Index ', I, ': ', A[i].GetType.FullName);
      ReadLn;
    end;

    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

    - Building Applications with Windows Workflow ...
    - Building the Data and Business Layers Using ...
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek