.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  
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 - 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

    - 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 5 hosted by Hostway
    Stay green...Green IT