.NET
  Home arrow .NET arrow Page 7 - The Delphi Language, Part 1
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 1
By: Xavier Pacheco
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 16
    2004-07-07

    Table of Contents:
  • The Delphi Language, Part 1
  • Procedures and Functions
  • Constants, Operators
  • Delphi Language Types
  • Variant Types
  • Variants in Expressions
  • Arrays
  • Records and Sets
  • Pointers
  • Classes and Objects

  • 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 1 - Arrays


    (Page 7 of 10 )

    The Delphi language enables you to create arrays of any type of variable. For example, a variable declared as an array of eight integers reads like this:

    var A:
      Array[0..7] of Integer;

    This statement is equivalent to the following C# declaration:

    int A[8];

    It's also equivalent to this Visual Basic .NET statement:

    Dim A(8) as Integer

    Delphi arrays have a special property that differentiates them from other languages: They don't have to be based at a certain number. You can therefore declare a three-element array that starts at 28, as in the following example:

    var
      A: Array[28..30] of Integer;

    Because Delphi arrays aren't guaranteed to begin at 0 or 1, you must use some care when iterating over array elements in a for loop. The compiler provides built-in functions called High() and Low(), which return the lower and upper bounds of an array variable or type, respectively. Your code will be less error prone and easier to maintain if you use these functions to control your for loop, as shown here:

    var
      A: array[28..30] of Integer;
     
    i: Integer;
    begin
      for i := Low(A) to High(A) do // don't hard-code for loop!
      A[i] := i;
    end;

    To specify multiple dimensions, use a comma-delimited list of bounds:

    var
      // Two-dimensional array of Integer:
     
    A: array[1..2, 1..2] of Integer;

    To access a multidimensional array, use commas to separate each dimension within one set of brackets:

    I := A[1, 2];

    Dynamic Arrays

    Dynamic arrays are dynamically allocated arrays in which the dimensions aren't known at compile time. To declare a dynamic array, just declare an array without including the dimensions, like this:

    var
      // dynamic array of string:
      SA: array of string;

    Before you can use a dynamic array, you must use the SetLength() procedure to allocate memory for the array:

    begin
      // allocate room for 33 elements:
      SetLength(SA, 33);

    Once memory has been allocated, you can access the elements of the dynamic array just like a normal array:

    SA[0] := 'Pooh likes hunny';
    OtherString := SA[0];

    Note - Dynamic arrays are always zero-based.

    Dynamic arrays are lifetime managed by the .NET runtime, so there's no need (and, in fact, no way) to free them when you're through using them because they will be garbage collected at some point after leaving scope. However, there might come a time when you want to request that the .NET runtime remove the dynamic array from memory before it leaves scope (if it uses a lot of memory, for example). To do this, you need only assign the dynamic array to nil:

    SA := nil; // requests release of SA

    Note that assigning to nil does not release SA; it only releases a reference to it, as there could be more than one variable referencing the array indicated by SA. When the last reference to SA is released, the .NET garbage collector will release the memory occupied by the array during the next garbage collection.

    Dynamic arrays are manipulated using reference semantics rather than value semantics like a normal array. Here's a quick test: What is the value of A1[0] at the end of the following code fragment?

    var
      A1, A2: array of Integer;
    begin
      SetLength(A1, 4);
      A2 := A1;
      A1[0] := 1;
      A2[0] := 26;

    The correct answer is 26 because the assignment A2 := A1 doesn't create a new array but instead provides A2 with a reference to the same array as A1. Therefore, any modifications to A2 will also affect A1. If you want instead to make a complete copy of A1 in A2, use the Copy() standard procedure:

    A2 := Copy(A1);

    After this line of code is executed, A2 and A1 will be two separate arrays initially containing the same data. Changes to one will not affect the other. You can optionally specify the starting element and number of elements to be copied as parameters to Copy(), as shown here:

    // copy 2 elements, starting at element one:
    A2 := Copy(A1, 1, 2);

    Dynamic arrays can also be multidimensional. To specify multiple dimensions, add an additional array of to the declaration for each dimension:

    var
      // two-dimensional dynamic array of Integer:
      IA: array of array of Integer;

    To allocate memory for a multidimensional dynamic array, pass the sizes of the other dimensions as additional parameters to SetLength():

    begin
      // IA will be a 5 x 5 array of Integer
      SetLength(IA, 5, 5);

    You access multidimensional dynamic arrays the same way you do normal multidimensional arrays; each element is separated by a comma with a single set of brackets:

    IA[0,3] := 28;

    C-style sytanx for multidimensional array access is also supported:

    IA[0][3] := 28;

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