.NET
  Home arrow .NET arrow Page 5 - 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 - Variant Types


    (Page 5 of 10 )

    The Variant class provides an implementation of a type capable of serving as a kind of container for many other types. This means that a variant can change the kind of data to which it refers at runtime. For example, the following code will compile and run properly:

    var
      V: Variant;
      I: IInterface;
    begin
      V := 'Delphi is Great!'; // Variant holds a string
      V := 1;                  // Variant now holds an Integer
      V := 123.34;       // Variant now holds a floating point
      V := True;         // Variant now holds a boolean
      V := I;            // Variant now holds an interface
    end;

    Variants support a wide variety of types, including complex types such as arrays and interfaces. The following code from the Borland.Vcl.Variants unit shows the various supported types, which are identified by values known as TVarTypes:

    type
      TVarType = Integer;

    const            // Decimal | Other names for this type
      varEmpty = $0000;    // = 0 | Unassigned, Nil
      varNull = $0001;     // = 1 | Null, System.DBNull
      varSmallInt = $0002; // = 2 | I2, System.Int16
      varInteger = $0003;  // = 3 | I4, System.Int32
      varSingle = $0004;   // = 4 | R4, System.Single
      varDouble = $0005;   // = 5 | R8, System.Double
      varCurrency = $0006; // = 6 | Borland.Delphi.System.Currency
      varDate = $0007; // = 7 | Borland.Delphi.System.TDateTime
      varString = $0008;   // = 8  | WideString, System.String
      varError = $000A;    // = 10 | Exception, System.Exception
      varBoolean = $000B;  // = 11 | Bool, System.Boolean
      varObject = $000C;   // = 12 | TObject, System.Object
      varDecimal = $000E;  // = 14 | System.Decimal
      varShortInt = $0010; // = 16 | I1, System.SByte
      varByte = $0011;     // = 17 | U1, System.Byte
      varWord = $0012;     // = 18 | U2, System.UInt16
      varLongWord = $0013; // = 19 | U4, System.UInt32
      varInt64 = $0014;    // = 20 | I8, System.Int64
      varUInt64 = $0015;   // = 21 | U8, System.UInt64
      varChar = $0016;     // = 22 | WideChar, System.Char
      varDateTime = $0017; // = 23 | System.DateTime;

      varFirst = varEmpty;
      varLast = varDateTime;

      varArray = $2000;   // = 8192 | System.Array, Dynamic   Arrays
      varTypeMask = $0FFF; // = 4095
      varUndefined = -1;

    As a function of value assignment, variants are capable of coercing themselves into other types, as needed. For example:

    var
      V: Variant; 
      I: Integer;
    begin
      V := '1'; // V hold string '1'
      I := V;   // Implicitly converts to Integer, I is now 1
    end;

    Typecasting Variants

    You can explicitly typecast expressions to type Variant. For example, the expression

    Variant(X);

    results in a Variant type whose type code corresponds to the result of the expression X, which must be an integer, floating point, currency, string, character, date/time, class, or Boolean type.

    You can also typecast a variant to that of a simple data type. For example, given the assignment

    V := 1.6;

    where V is a variable of type Variant, the following expressions will have the following results shown:

    S := string(V);  // S will contain the string '1.6';
    I := Integer(V); // I is rounded to the nearest Integer value, in this case: 2.
    B := Boolean(V); // B contains True, since V is not equal to 0
    D := Double(V);  // D contains the value 1.6

    These results are dictated by certain type-conversion rules applicable to Variant types. These rules are defined in detail in the Delphi Language Guide.

    By the way, in the preceding example, it isn't necessary to typecast the variant to another data type to make the assignment. The following code would work just as well:

    V := 1.6;
    S := V;
    I := V;
    B := V;
    D := V;

    When multiple variants are used in an expression, there can be much more behind-the-scenes code logic attached to these implicit type coercions. In such cases, if you're sure of the type a variant contains, you're better off explicitly typecasting it to that type in order to speed up the operation.

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