In this section, we will explore two important data types in Delphi/Object Pascal: Enumerated Types and Subrange Types. These types help in creating more readable and maintainable code by allowing you to define a set of named values and restrict the range of values a variable can hold.

Enumerated Types

Definition

An enumerated type is a user-defined data type that consists of a set of named values called elements. These elements are ordered and have an ordinal value starting from 0.

Syntax

type
  TColor = (Red, Green, Blue, Yellow);

Example

Let's create a simple program that uses an enumerated type to represent colors.

program EnumeratedExample;

type
  TColor = (Red, Green, Blue, Yellow);

var
  Color: TColor;

begin
  Color := Red;
  WriteLn('The selected color is: ', Ord(Color)); // Output: The selected color is: 0

  Color := Blue;
  WriteLn('The selected color is: ', Ord(Color)); // Output: The selected color is: 2
end.

Explanation

  • TColor is an enumerated type with four elements: Red, Green, Blue, and Yellow.
  • Ord(Color) returns the ordinal value of the enumerated element.

Practical Use Case

Enumerated types are useful for representing a fixed set of related constants, such as days of the week, states in a state machine, or options in a menu.

Exercise

Create an enumerated type TDay to represent the days of the week. Write a program that prints the ordinal value of each day.

program DaysOfWeek;

type
  TDay = (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);

var
  Day: TDay;

begin
  for Day := Monday to Sunday do
    WriteLn('The ordinal value of ', Day, ' is: ', Ord(Day));
end.

Solution

program DaysOfWeek;

type
  TDay = (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);

var
  Day: TDay;

begin
  for Day := Monday to Sunday do
    WriteLn('The ordinal value of ', Day, ' is: ', Ord(Day));
end.

Subrange Types

Definition

A subrange type is a subset of an existing ordinal type, which can be an integer, character, or enumerated type. It restricts the range of values a variable can hold.

Syntax

type
  TAge = 0..120;

Example

Let's create a simple program that uses a subrange type to represent age.

program SubrangeExample;

type
  TAge = 0..120;

var
  Age: TAge;

begin
  Age := 25;
  WriteLn('The age is: ', Age); // Output: The age is: 25

  // Uncommenting the following line will cause a compilation error
  // Age := 130; // Error: constant expression violates subrange bounds
end.

Explanation

  • TAge is a subrange type that restricts the values to be between 0 and 120.
  • Assigning a value outside this range will result in a compilation error.

Practical Use Case

Subrange types are useful for ensuring that variables only hold valid values, such as age, scores, or any other bounded numerical data.

Exercise

Create a subrange type TScore to represent scores between 0 and 100. Write a program that assigns a valid score and prints it.

program ScoreExample;

type
  TScore = 0..100;

var
  Score: TScore;

begin
  Score := 85;
  WriteLn('The score is: ', Score); // Output: The score is: 85

  // Uncommenting the following line will cause a compilation error
  // Score := 105; // Error: constant expression violates subrange bounds
end.

Solution

program ScoreExample;

type
  TScore = 0..100;

var
  Score: TScore;

begin
  Score := 85;
  WriteLn('The score is: ', Score); // Output: The score is: 85

  // Uncommenting the following line will cause a compilation error
  // Score := 105; // Error: constant expression violates subrange bounds
end.

Summary

In this section, we covered:

  • Enumerated Types: How to define and use them to create a set of named values.
  • Subrange Types: How to define and use them to restrict the range of values a variable can hold.

These types help in creating more readable, maintainable, and error-resistant code. In the next section, we will explore file handling in Delphi/Object Pascal.

Delphi/Object Pascal Programming Course

Module 1: Introduction to Delphi/Object Pascal

Module 2: Control Structures and Procedures

Module 3: Working with Data

Module 4: Object-Oriented Programming

Module 5: Advanced Delphi Features

Module 6: GUI Development with VCL and FMX

Module 7: Web and Mobile Development

Module 8: Best Practices and Design Patterns

Module 9: Final Project

© Copyright 2024. All rights reserved