Difference between revisions of "Records"

From Lazarus wiki
m (Fixed syntax highlighting)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
{{Records}}
 +
{{TYNavigator|Multidimensional_arrays|Pointers}}
 +
 
5E - Records (author: Tao Yue, state: unchanged)
 
5E - Records (author: Tao Yue, state: unchanged)
  
Line 4: Line 7:
  
 
To declare a record, you'd use:
 
To declare a record, you'd use:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
TYPE
 
TYPE
 
   TypeName = record
 
   TypeName = record
Line 14: Line 18:
  
 
For example:
 
For example:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
type
 
type
 
   InfoType = record
 
   InfoType = record
Line 25: Line 30:
  
 
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:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
  VariableIdentifier.FieldIdentifier
 
  VariableIdentifier.FieldIdentifier
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 32: Line 38:
  
 
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:
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
WITH RecordVariable DO
 
WITH RecordVariable DO
 
BEGIN
 
BEGIN
Line 40: Line 46:
  
 
Example:
 
Example:
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
with Info do
 
with Info do
 
begin
 
begin
Line 48: Line 55:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
{|style=color-backgroud="white" cellspacing="20"
+
{{TYNavigator|Multidimensional_arrays|Pointers}}
|[[Multidimensional_arrays|previous]] 
 
|[[Contents|contents]]
 
|[[Pointers|next]]
 
|}
 
 
 
[[Category: Object Pascal Introduction]]
 

Latest revision as of 08:26, 25 February 2020

English (en) français (fr) 日本語 (ja) 中文(中国大陆)‎ (zh_CN)

 ◄   ▲   ► 

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:

TYPE
  TypeName = record
    identifierlist1 : datatype1;
    ...
    identifierlistn : datatypen;
  end;

For example:

type
  InfoType = record
    Name : string;
    Age : integer;
    City, State : String;
    Zip : integer;
  end;

Each of the identifiers Name, Age, City, State, and Zip are referred to as fields. You access a field within a variable by:

 VariableIdentifier.FieldIdentifier

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:

WITH RecordVariable DO
BEGIN
  ...
END;

Example:

with Info do
begin
  Age := 18;
  ZIP := 90210;
end;
 ◄   ▲   ►