Difference between revisions of "Basic Pascal Tutorial/Chapter 5/Records"

From Free Pascal wiki
Jump to navigationJump to search
m (Kai Burghardt moved page Records to Basic Pascal Tutorial/Chapter 5/Records: tidy up main name space: create subpage hierarchy for basic Pascal tutorial [cf. [[Special: PermaLink/149778#Cluttering of mai...)
(bypass redirects [cf. discussion])
Line 1: Line 1:
 
{{Records}}
 
{{Records}}
{{TYNavigator|Multidimensional_arrays|Pointers}}
+
{{TYNavigator|Chapter 5/Multidimensional arrays|Chapter 5/Pointers}}
  
 
5E - Records (author: Tao Yue, state: unchanged)
 
5E - Records (author: Tao Yue, state: unchanged)
Line 55: Line 55:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
{{TYNavigator|Multidimensional_arrays|Pointers}}
+
{{TYNavigator|Chapter 5/Multidimensional arrays|Chapter 5/Pointers}}

Revision as of 02:09, 6 August 2022

български (bg) 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;
 ◄   ▲   ►