Structure Type (1)

Source: Internet
Author: User
Record type

The record type is used to define a fixed set of different types of data items. Each element or field in the record has its own type. All domains are listed in the record type definition. Each domain corresponds to one domain name and can be accessed through the domain name.

The following briefly lists the definition of record types, the declaration of type variables, and the use of such variables:

type  Date = record    Year: Integer;    Month: Byte;    Day: Byte;  end;  var  BirthDay: Date;  begin  BirthDay.Year := 1997;  BirthDay.Month := 2;  BirthDay.Day := 14;

Classes and objects can be seen as extensions of record types. The Delphi library tends to replace record types with classes, but many record types are defined in Windows APIs.

The record type can contain the variant field, which indicates that multiple domains can share the same memory zone, and the field can be of different types (which corresponds to the union in C ). In other words, you can access the same memory location through the variant domain or a group of domain access records, but each value still needs to be treated differently. The variant type is mainly used to store similar but different data and perform type conversion similar to typecasting (this method is rarely used since the introduction of typecasting to Pascal ). Although Delphi still uses the variant record type in some special cases, it is now replaced by object-oriented technology or other modern technology.

Applications of the variant record type do not comply with the type security principles, so they are not recommended for programming. This is especially true for beginners. In fact, expert programmers really need to use the variant record type, which is used in the core part of the Delphi Database. However, unless you are a Delphi expert, you should avoid using the variant record type.

Else ---------------------------------------------------------------------------------------------------------------------------------

In case of a blog

// If you want to record a person's name and age, you can:
ProcedureTForm1.Button1Click (Sender: TObject );
Var
Name:String;
Age: Word;
Begin
{Assignment}
Name: = 'zhang san ';
Age: = 18;

{Read}
ShowMessage (Format ('% s % d years old', [name, age]); {Zhang San, 18 years old}
End;

// It is better to define a structure type.
ProcedureTForm1.Button2Click (Sender: TObject );
Type
TPerson =Record
Name:String[12]; {long strings cannot be used in the structure}
Age: Word;
End;
Var
Person: TPerson; {declare Structure Variable}
Const
Str = '% s % d'; {prepare a constant for formatted output}
Begin
{Assignment}
Person. name: = 'Li si ';
Person. age: = 81;

{Read}
ShowMessage (Format (str, [person. name, person. age]); {Li Si, 81 years old}
End;

Bytes ------------------------------------------------------------------------------------------------------

Compression Structure

Type
TRec1 = record
I: Integer;
W: Word;
End;

TRec2 = packed record {compression structure: Sacrifice efficiency, reduce size}
I: Integer;
W: Word;
End;

Procedure TForm1.Button1Click (Sender: TObject );
Begin
ShowMessage (IntToStr (SizeOf (TRec1); {8}
ShowMessage (IntToStr (SizeOf (TRec2); {6}
End;
Bytes ------------------------------------------------------------------------------------------------------

Relationship Between Structures

Type
TRec1 = record
Name: string [12];
Age: Word;
End;

TRec2 = record
Name: string [12];
Age: Word;
End;

Var
RecA, RecB: TRec1;
RecX, RecY: TRec2;

Procedure TForm1.Button1Click (Sender: TObject );
Begin
{Assign a value to RecA}
RecA. name: = 'zhang san ';
RecA. age: = 18;

{Copy RecA to RecB}
RecB: = RecA;

{The RecB value is the same as that of RecA}
ShowMessage (RecB. name); {Zhang San}

{Modify RecB value}
RecB. name: = 'Li si ';

{The RecA value remains unchanged, because RecA and RecB are two different data records}
ShowMessage (RecA. name); {Zhang San}

// RecX: = RecA; {this will cause an error !}
{Although RecX and RecA have exactly the same internal structure, Delphi regards them as different data types}

RecX: = TRec2 (RecA); {but can be forcibly converted}
ShowMessage (RecX. name); {Zhang San}

RecY. name: = RecB. name; {an internal value can be exchanged}
RecY. age: = RecA. age;
ShowMessage (RecY. name); {Li Si}
End;

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.