Difference between revisions of "Records"

From Lazarus wiki
m (Fixed syntax highlighting)
 
(7 intermediate revisions by 5 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:
<font color="#006699"><strong>TYPE</strong></font>
+
 
  TypeName <font color="#000000"><strong>=</strong></font> <font color="#006699"><strong>record</strong></font>
+
<syntaxhighlight lang=pascal>
      identifierlist1 <font color="#000000"><strong>:</strong></font> datatype1<font color="#000000"><strong>;</strong></font>
+
TYPE
      <font color="#000000"><strong>.</strong></font><font color="#000000"><strong>.</strong></font><font color="#000000"><strong>.</strong></font>
+
  TypeName = record
      identifierlistn <font color="#000000"><strong>:</strong></font> datatypen<font color="#000000"><strong>;</strong></font>
+
    identifierlist1 : datatype1;
  <font color="#006699"><strong>end</strong></font><font color="#000000"><strong>;</strong></font>
+
    ...
 +
    identifierlistn : datatypen;
 +
  end;
 +
</syntaxhighlight>
  
 
For example:
 
For example:
<font color="#006699"><strong>type</strong></font>
+
 
  InfoType <font color="#000000"><strong>=</strong></font> <font color="#006699"><strong>record</strong></font>
+
<syntaxhighlight lang=pascal>
      <font color="#009966"><strong>Name</strong></font> <font color="#000000"><strong>:</strong></font> <font color="#006699"><strong>string</strong></font><font color="#000000"><strong>;</strong></font>
+
type
      Age <font color="#000000"><strong>:</strong></font> <font color="#0099ff"><strong>integer</strong></font><font color="#000000"><strong>;</strong></font>
+
  InfoType = record
      City<font color="#000000"><strong>,</strong></font> State <font color="#000000"><strong>:</strong></font> <font color="#006699"><strong>String</strong></font><font color="#000000"><strong>;</strong></font>
+
    Name : string;
      Zip <font color="#000000"><strong>:</strong></font> <font color="#0099ff"><strong>integer</strong></font><font color="#000000"><strong>;</strong></font>
+
    Age : integer;
  <font color="#006699"><strong>end</strong></font><font color="#000000"><strong>;</strong></font>
+
    City, State : String;
 +
    Zip : integer;
 +
  end;
 +
</syntaxhighlight>
  
 
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 lang=pascal>
 
  VariableIdentifier.FieldIdentifier
 
  VariableIdentifier.FieldIdentifier
 +
</syntaxhighlight>
  
 
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:
<font color="#006699"><strong>WITH</strong></font> RecordVariable <font color="#006699"><strong>DO</strong></font>
+
<syntaxhighlight lang=pascal>
<font color="#006699"><strong>BEGIN</strong></font>
+
WITH RecordVariable DO
  <font color="#000000"><strong>.</strong></font><font color="#000000"><strong>.</strong></font><font color="#000000"><strong>.</strong></font>
+
BEGIN
<font color="#006699"><strong>END</strong></font><font color="#000000"><strong>;</strong></font>
+
  ...
 +
END;
 +
</syntaxhighlight>
  
 
Example:
 
Example:
<font color="#006699"><strong>with</strong></font> Info <font color="#006699"><strong>do</strong></font>
 
<font color="#006699"><strong>begin</strong></font>
 
  Age <font color="#000000"><strong>:=</strong></font> <font color="#ff0000">18</font><font color="#000000"><strong>;</strong></font>
 
  ZIP <font color="#000000"><strong>:=</strong></font> <font color="#ff0000">90210</font><font color="#000000"><strong>;</strong></font>
 
<font color="#006699"><strong>end</strong></font><font color="#000000"><strong>;</strong></font>
 
  
{|style=color-backgroud="white" cellspacing="20"
+
<syntaxhighlight lang=pascal>
|[[Multidimensional_arrays|previous]] 
+
with Info do
|[[op_contents|contents]]
+
begin
|[[Pointers|next]]
+
  Age := 18;
|}
+
  ZIP := 90210;
 +
end;
 +
</syntaxhighlight>
 +
 
 +
{{TYNavigator|Multidimensional_arrays|Pointers}}

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;
 ◄   ▲   ►