.NET
  Home arrow .NET arrow Page 6 - 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  
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 1
By: Xavier Pacheco
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 17
    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 - Variants in Expressions


    (Page 6 of 10 )

    You can use variants in expressions with the following operators: +, =, *, /, div, mod, shl, shr, and, or, xor, not, :=, <>, <, >, <=, and >=. The standard Inc(), Dec(), Trunc(), and Round() functions are also valid with variant expressions.

    When using variants in expressions, Delphi knows how to perform the operations based on the types contained within a variant. For example, if two variants, V1 and V2, contain integers, the expression V1 + V2 results in the addition of the two integers. However, if V1 and V2 contain strings, the result is a concatenation of the two strings. What happens if V1 and V2 contain two different data types? Delphi uses certain promotion rules in order to perform the operation. For example, if V1 contains the string '4.5' and V2 contains a floating-point number, V1 will be converted to a floating point and then added to V2. The following code illustrates this:

    var
      V1, V2, V3: Variant;
    begin
      V1 := '100'; // A string type
      V2 := '50';  // A string type
      V3 := 200;   // An Integer type
      V1 := V1 + V2 + V3;
    end;

    Based on what we just mentioned about promotion rules, it would seem at first glance that the preceding code would result in the value 350 as an integer. However, if you take a closer look, you'll see that this is not the case. Because the order of precedence is from left to right, the first equation executed is V1 + V2. Because these two variants refer to strings, a string concatenation is performed, resulting in the string '10050'. That result is then added to the integer value held by the variant V3. Because V3 is an integer, the result '10050' is converted to an integer and added to V3, thus providing an end result of 10250.

    Delphi promotes the variants to the numerically largest type in the equation in order to successfully carry out the calculation. However, when an operation is attempted on two variants of which Delphi cannot make any sense, an EVariantTypeCast exception is raised. The following code illustrates this:

    var
      V1, V2: Variant;
    begin
      V1 := 77;
      V2 := 'hello';
      V1 := V1 / V2; // Raises an exception.
    end;

    As stated earlier, it's sometimes a good idea to explicitly typecast a variant to a specific data type if you know what that type is and if it's used in an expression. Consider the following line of code:

    V4 := V1 * V2 / V3;

    Before a result can be generated for this equation, each operation is handled by a runtime function that goes through several gyrations to determine the compatibility of the types the variants represent. Then the conversions are made to the appropriate data types. This results in a large amount of overhead and code size. A better solution is obviously not to use variants. However, when necessary, you can also explicitly typecast the variants so the operations are resolved at compile time:

    V4 := Integer(V1) * Double(V2) / Integer(V3);

    Keep in mind that this assumes you know the data types to which the variants can be successfully converted.

    Empty and Null

    Two special values for variants merit a brief discussion. The first is Unassigned, which means that the variant has not yet been assigned a value. This is the initial value of the variant as it comes into scope. The other is Null, which is different from Unassigned in that it actually represents the value Null as opposed to a lack of value. This distinction between no value and a Null value is especially important when applied to the field values of a database table. You will learn about database programming in Part IV, "Database Development with ADO.NET."

    Another difference is that attempting to perform operations with a variant containing an Unassigned value will result in the value being converted to 0 for numeric operations or an empty string for string operations. The same isn't true of variants containing a Null value, however. When a variant involved in an equation contains a Null value, operating on the variant with that value might result in an EVariantTypeCast exception being raised.

    If you want to assign or compare a variant to one of these two special values, the System.Vcl.Variants unit defines two variants, appropriated called Unassigned and Null, which can be used for assignment and comparison.


    Controlling Variant Behavior - You can control whether an exception is raised when performing an arithmetic operation with a Null variant by setting the Variant.NullStrictOperations property to True. The other Variant flags controlling Null behavior and string and Boolean conversions include

    NullEqualityRule: TNullCompareRule;
    NullMagnitudeRule: TNullCompareRule;
    NullAsStringValue: string;
    NullStrictConvert: Boolean;
    BooleanToStringRule: TBooleanToStringRule;
    BooleanTrueAsOrdinalValue: Integer;


    User-Defined Types

    Integers, strings, and floating-point numbers often are not enough to adequately represent variables in the real-world problems that programmers must try to solve. In cases like these, you must create your own types to better represent variables in the current problem. In Delphi, these user-defined types usually come in the form of records or classes; you declare these types using the Type keyword.

    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

    - 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...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT