Difference between revisions of "Basic Pascal Tutorial/Chapter 5/Records"
Line 4: | Line 4: | ||
To declare a record, you'd use: | To declare a record, you'd use: | ||
− | + | <delphi> | |
− | + | TYPE | |
− | + | TypeName = record | |
− | + | identifierlist1 : datatype1; | |
− | + | ... | |
− | + | identifierlistn : datatypen; | |
+ | end; | ||
+ | </delphi> | ||
For example: | For example: | ||
− | + | <delphi> | |
− | + | type | |
− | + | InfoType = record | |
− | + | Name : string; | |
− | + | Age : integer; | |
− | + | City, State : String; | |
− | + | Zip : integer; | |
+ | end; | ||
+ | </delphi> | ||
Each of the identifiers <tt>Name, Age, City, State</tt>, and <tt>Zip</tt> are referred to as fields. You access a field within a variable by: | Each of the identifiers <tt>Name, Age, City, State</tt>, and <tt>Zip</tt> are referred to as fields. You access a field within a variable by: | ||
+ | <delphi> | ||
VariableIdentifier.FieldIdentifier | VariableIdentifier.FieldIdentifier | ||
+ | </delphi> | ||
A period separates the variable and the field name. | A period separates the variable and the field name. | ||
There's a very useful statement for dealing with records. If you are going to be using one record variable for a long time and don't feel like typing the variable name over and over, you can strip off the variable name and use only field identifiers. You do this by: | There's a very useful statement for dealing with records. If you are going to be using one record variable for a long time and don't feel like typing the variable name over and over, you can strip off the variable name and use only field identifiers. You do this by: | ||
− | + | <delphi> | |
− | + | WITH RecordVariable DO | |
− | + | BEGIN | |
− | + | ... | |
+ | END; | ||
+ | </delphi> | ||
Example: | Example: | ||
− | + | <delphi> | |
− | + | with Info do | |
− | + | begin | |
− | + | Age := 18; | |
− | + | ZIP := 90210; | |
+ | end; | ||
+ | </delphi> | ||
{|style=color-backgroud="white" cellspacing="20" | {|style=color-backgroud="white" cellspacing="20" |
Revision as of 17:07, 6 January 2010
5E - Records (author: Tao Yue, state: unchanged)
A record allows you to keep related data items in one structure. If you want information about a person, you may want to know name, age, city, state, and zip.
To declare a record, you'd use: <delphi> TYPE
TypeName = record identifierlist1 : datatype1; ... identifierlistn : datatypen; end;
</delphi>
For example: <delphi> type
InfoType = record Name : string; Age : integer; City, State : String; Zip : integer; end;
</delphi>
Each of the identifiers Name, Age, City, State, and Zip are referred to as fields. You access a field within a variable by: <delphi>
VariableIdentifier.FieldIdentifier
</delphi>
A period separates the variable and the field name.
There's a very useful statement for dealing with records. If you are going to be using one record variable for a long time and don't feel like typing the variable name over and over, you can strip off the variable name and use only field identifiers. You do this by: <delphi> WITH RecordVariable DO BEGIN
...
END; </delphi>
Example: <delphi> with Info do begin
Age := 18; ZIP := 90210;
end; </delphi>
previous | contents | next |