Array Types

Array types defines the list of elements with a certain type. You can usually use the index in square brackets to access one of the elements array. Angle brackets are also used to determine the value of the index when possible to define arrays. For example, you can specify a group that consists of 24 integer with the code as follows

type
DayTemperatures = array [1..24] of Integer;

In the array definition, you must spend a subrange type in square brackets, or define a subrange type that uses two new constant with Ordinal type. Subrange this will determine the array index is valid. Because you determine the value of the index up and down from the array, the index does not need to be based from the value 0, such as that required in C, C + +, Java, and other programming languages.

Because the index is based on the array subrange, Delphi can check their range as we have seen before. A constant subrange invalid will generate an error during the compilation, and when we use an index that exceeds the range previously determined, the error message will appear when the application is run if the choice of compiling the relevant activated.

Using the above definition array, you can fill in the value of a variable DayTempl with type DayTemperaturs such as the following:

type
DayTemperatures = array [1..24] of Integer;

var
DayTemp1: DayTemperatures;

procedure AssignTemp;
begin
DayTemp1 [1] := 54;
DayTemp1 [2] := 52;
...
DayTemp1 [24] := 66;
DayTemp1 [25] := 67; // compile-time error
An array can have more than one dimension, as in the following examples:

type
MonthTemps = array [1..24, 1..31] of Integer;
YearTemps = array [1..24, 1..31, Jan..Dec] of Integer;
This second type arrays built with the same basic type. So you can use mendeklarasikannya type of data before, such as the following examples:

type
MonthTemps = array [1..31] of DayTemperatures;
YearTemps = array [Jan..Dec] of MonthTemps;
The declaration change the order of the index, such as in the example above, but also allows charging the variables in one block, intact. For example, the following command will copy the temperature January to February:

var
ThisYear: YearTemps;
begin
...
ThisYear[Feb] := ThisYear[Jan];

You can also define a zero-based arrays, a type array with the limits set down to zero. In general, which limits the use of more logical, because we do not need to use the index 2 to access the items to three, and so on. However, Windows uses the many zero-based arrays (because it is based on the language C) and component library Delphi also tended to the case.

If you need to work on the array, you can always check the limitations with using the standard functions Low and High, a return below the limit value and above. Using Low and High when operating the array is recommended, especially in the loop (repetition), because we do not create a code depending on the range of arrays that are used. Next, you can change the range that has been declared, and the code that uses Low and High can still work. If you write code that uses the value in a range of static arrays, you must update the code when the array size changes. Low and High make your code more easily maintained and stable.

See examples of the use of the array here

Postingan populer dari blog ini

vb6 msflexgrid

Tutorial Visual Basic 6.0 : Listbox in VB6

How to Use Mshflexgrid