.NET
  Home arrow .NET arrow Page 8 - 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 - Records and Sets


    (Page 8 of 10 )

    A user-defined structure is referred to as a record in the Delphi language, and it's the equivalent of C#'s struct or Visual Basic .NET's Type. As an example, here's a record definition in Delphi as well as equivalent definitions in C# and Visual Basic .NET:

    { Delphi }
    Type
      MyRec = record
        i: Integer;
        d: Double;
      end; 
    /* C# */ 
    public struct MyRec 

      int i; 
     
    double d; 
    }

    'Visual Basic
    Type MyRec
      i As Integer
      d As Double
    End Type

    When working with a record, you use the dot symbol to access its fields. Here's an example:

    var
      N: MyRec;
    begin
      N.i := 23;
      N.d := 3.4;
    end;

    Methods, operator overloads, and interfaced implementations are also supported for records. This capability was not supported on previous versions of the Delphi compiler. These topics are covered in more detail, along with class types, later in this chapter. However, the following code example shows the syntax for using these elements with records:

    IBlah = interface
      procedure bar;
    end;

    Foo = record(IBlah) // record implements IBlah interface
      AField: Integer;
      procedure bar;
      class operator Add(a, b: Foo): Foo; // overload + operator
    end;

    Sets

    Sets are a uniquely Delphi type that have no equivalent in C# or Visual Basic .NET. Sets provide an efficient and convenient means of representing a collection of ordinal, AnsiChar, or enumerated values. You can declare a new set type using the keywords set of followed by an ordinal type or subrange of possible set values. Here's an example:

    type
      TCharSet = set of AnsiChar; // possible members: #0 - #255
      TEnum = (Monday, Tuesday, Wednesday, Thursday, Friday);
      TEnumSet = set of TEnum; // can contain any combination of TEnum members
      TSubrangeSet = set of 1..10; // possible members: 1 - 10

    Note that a set can only contain up to 256 elements. Additionally, only ordinal types can follow the set of keywords. Therefore, the following declarations are illegal:

    type
      TIntSet = set of Integer; // Invalid: too many elements
      TStrSet = set of string; // Invalid: not an ordinal type

    Sets store their elements internally as individual bits, which makes them very efficient in terms of speed and memory usage.


    Note - If you are porting Win32 code to .NET, bear in mind that chars are 2-bytes in the .NET world and 1-byte in Win32. This means that a set of Char declaration in .NET is demoted by the compiler to a set of AnsiChar, which can potentially change the meaning of code. The compiler will issue a warning to this effect, recommending that you explicitly use set of AnsiChar.


    Using Sets

    Use square brackets when constructing a literal set value from one or more elements. The following code demonstrates how to declare set type variables and assign them values:

    type
      TCharSet = set of AnsiChar; // possible members: #0 - #255
      TEnum = (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);
      TEnumSet = set of TEnum; // can contain any combination of TEnum members

    var
      CharSet: TCharSet;
      EnumSet: TEnumSet;
      SubrangeSet: set of 1..10; // possible members: 1 - 10
      AlphaSet: set of 'A'..'z'; // possible members: 'A' - 'z'

    begin
      CharSet := ['A'..'J', 'a', 'm'];
      EnumSet := [Saturday, Sunday];
      SubrangeSet := [1, 2, 4..6];
      AlphaSet := []; // Empty; no elements
    end;

    Set Operators

    The Delphi language provides several operators for use in manipulating sets. You can use these operators to determine set membership, union, difference, and intersection.

    Membership

    Use the in operator to determine whether a given element is contained in a particular set. For example, the following code would be used to determine whether the CharSet set mentioned earlier contains the letter 'S':

    if 'S' in CharSet then
      // do something;

    The following code determines whether EnumSet lacks the member Monday:

    if not (Monday in EnumSet) then
      // do something;

    Union and Difference

    Use the + and - operators or the Include() and Exclude() procedures to add and remove elements to and from a set variable:

    Include(CharSet, 'a');           // add 'a' to set
    CharSet := CharSet + ['b'];      // add 'b' to set
    Exclude(CharSet, 'x');           // remove 'z' from set
    CharSet := CharSet - ['y', 'z']; // remove 'y' and 'z' from set

    Tip - When possible, use Include() and Exclude() to add and remove a single element to and from a set rather than the + and operators, as the former is more efficient.


    Intersection

    Use the * operator to calculate the intersection of two sets. The result of the expression Set1 * Set2 is a set containing all the members that Set1 and Set2 have in common. For example, the following code could be used as an efficient means for determining whether a given set contains multiple elements:

    if ['a', 'b', 'c'] * CharSet = ['a', 'b', 'c'] then
      // do something

    Unsafe Code

    Right about now, those with previous experience in Delphi for Win32 might be thinking, "this all sounds fine so far, but what happened to the pointers?" Although pointers are in the language, they are considered unsafe from a .NET standpoint because they allow for direct access to memory. Therefore, in order to employ pointers in your applications, you will need to let the compiler know that it should permit unsafe code. In order to write unsafe code, you must

    1. Include the {$UNSAFECODE ON} directive in the unit containing the unsafe code.

    2. Mark functions containing unsafe code with the unsafe keyword.

    The following unsafe code will successfully compile in Delphi:

    {$UNSAFECODE ON}

    procedure RunningWithScissors; unsafe;
    var
      A: array[0..31] of Char;
      P: PChar; // PChar is an unsafe type
    begin
      A := 'safety first'; // fill character array
      P := @A[0];          // point to first element
      P[0] := 'S';         // change first element
      MessageBox.Show(A);  // show changed array
    end;

    Note the use of Delphi's @, or address of operator, to obtain the address of a bit of data.


    Tip - Unsafe code is totally discouraged in .NET because an unsafe application will not pass muster with .NET's PEVerify tool and may therefore be subject to greater security restrictions. However, unsafe code can be an excellent way to bridge the gap between the Win32 world and the .NET world during a migration period. It might be difficult to port an entire large application from Win32 to .NET in one fell swoop; however, unsafe code allows you to port one portion at a time, keeping the old code as unsafe until it can be moved in its entirety to safe code.


    You can assume that the code in this portion of the chapter must be compiled using the $UNSAFECODE and unsafe directives.

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