Delphi forced type conversions and type conventions

Source: Internet
Author: User

A technique that enforces type conversions by enabling the compiler to treat one type of variable as a different type.

because Pascal has the ability to define new types, the compiler checks for parameters and argument types that are very strict when invoking a function. Therefore, in order to be able to check through the compiler, it is often necessary to convert the type of one variable to the type of another variable. For example, suppose you want to assign a value of a character type to a variable of type byte:

var    c:char;    B:byte;begin    c:= ' a ';    b:= C;    The compiler will prompt for error end.

In the following code, forcing a type conversion to convert C to a byte type, actually forcing the type conversion is to tell the compiler that you know what you are doing and that you want to convert one type to another type :

var    c:char;    B:byte;begin    c:= ' s ';    b:= byte (c);    The compiler does not error the end.

Note: You can force type conversions on variables only if the data lengths of the two types are the same. For example, you cannot cast a double to an integer.

In order to convert a floating-point type to an integer, use the trunc () or round () function.

To convert an integral type to a value of a floating-point number type, use the following assignment statement:

floatvar:= intvar;//directly assigns an integer variable to the double type variable

  

Delphi forced type conversions and type conventions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.