How to Declare and Initialize Constant Arrays in Delphi

How to Declare and Initialize Constant Arrays in Delphi

We are searching data for your request:

Forums and discussions:
Manuals and reference books:
Data from registers:
Wait the end of the search in all databases.
Upon completion, a link will appear to access the found materials.

In Delphi, the versatile web-programming language, arrays allow a developer to refer to a series of variables by the same name and to use a number-an index-to tell them apart.

In most scenarios, you declare an array as a variable, which allows for array elements to be changed at run-time.

However, sometimes you need to declare a constant array-a read-only array. You cannot change the value of a constant or a read-only variable. Therefore, while declaring a constant array, you must also initialize it.

Example Declaration of Three Constant Arrays

This code example declares and initializes three constant arrays, named Days, CursorMode, and Items.

  • Days is a string array of six elements. Days1 returns the Mon string.
  • CursorMode is an array of two elements, whereby declaration CursorModefalse = crHourGlass and CursorMode = crSQLWait. "cr*" constants can be used to change the current screen cursor.
  • Items defines an array of three TShopItem records.

TShopItem = record
Name : string;
Price : currency;
Days : array0… 6 of string =
'Sun', 'Mon', 'Tue', 'Wed',
'Thu', 'Fri', 'Sat'
   ) ;
CursorMode : arrayboolean of TCursor =
crHourGlass, crSQLWait
   ) ;
Items : array1… 3 of TShopItem =
(Name : 'Clock'; Price : 20.99),
(Name : 'Pencil'; Price : 15.75),
(Name : 'Board'; Price : 42.96)
   ) ;

Trying to assign a value for an item in a constant array raises the "Left side cannot be assigned to" compile time error. For example, the following code does not successfully execute:

Items1.Name := 'Watch'; //will not compile