Sunday, October 20, 2019
How to Declare and Initialize Constant Arrays in Delphi
How to Declare and Initialize Constant Arrays in Delphi 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. Days[1] returns the Mon string.CursorMode is anà array of two elements, whereby declaration CursorMode[false] crHourGlass and CursorMode crSQLWait. cr* constants can be used to change the current screen cursor.Items defines an array of three TShopItemà records. type à à TShopItem record à à à à Name : string; à à à à Price : currency; à à end; const à à Days : array[0..6] of string à à ( à à à à à Sun, Mon, Tue, Wed, à à à à à Thu, Fri, Sat à à ) ; à à CursorMode : array[boolean] of TCursor à à ( à à à à crHourGlass, crSQLWait à à ) ; à à Items : array[1..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: Items[1].Name : Watch; //will not compile
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.