Delphi Language Guide Delphi for Microsoft Win32 Delphi for the Microsoft. Net framework



Yüklə 0,84 Mb.
Pdf görüntüsü
səhifə93/294
tarix02.01.2022
ölçüsü0,84 Mb.
#41395
1   ...   89   90   91   92   93   94   95   96   ...   294
DelphiLanguageGuide

Array Types and Assignments
Arrays are assignment-compatible only if they are of the same type. Because the Delphi language uses name-
equivalence for types, the following code will not compile.
var
  Int1: array[1..10] of Integer;
  array[1..10] of Integer;
      ...
  Int1 := Int2;
80


To make the assignment work, declare the variables as
var Int1, Int2: array[1..10] of Integer;
or
type IntArray = array[1..10] of Integer;
var
   Int1: IntArray;
   Int2: IntArray;
Dynamically allocated multidimensional arrays (.NET)
On the .NET platform, multidimensional arrays can be dynamically allocated using the New standard function. Using
the New syntax to allocate an array, the array declaration specifies the number of dimensions, but not their actual
size. You then pass the element type, the actual array dimensions, or an array initializer list to the New function. The
array declaration has the following syntax:
array[
, ..., 
] of 
 baseType;
In the syntax, note that the number of dimensions are specified by using a comma as a placeholder; the actual size
is not determined until runtime, when you call the New function. There are two forms of the New function: one takes
the element type and the size of the array, and the other takes the element type and an array initializer list. The
following code demonstrates both forms:
var
  a: array [,,] of integer;  // 3 dimensional array
  b: array [,] of integer;   // 2 dimensional array
  c: array [,] of TPoint;    // 2 dimensional array of TPoint
begin
   a := New(array[3,5,7] of integer);                 // New taking element type and size 
of each dimension.
   b := New(array[,] of integer, ((1,2,3), (4,5,6))); // New taking the element type and 
initializer list.
                                                      // New taking an initializer list of TPoint.
   c := New(array[,] of TPoint, (((X:1;Y:2), (X:3;Y:4)), ((X:5;Y:6), (X:7;Y:8))));
end.
You can allocate the array by passing variable or constant expressions to the New function:
var
   a:   array[,] of integer; 
   r,c: Integer;
begin
   r := 4;
   c := 17;
   a := New(array [r,c] of integer);
You can also use the SetLength procedure to allocate the array, by passing the array expression, and the size of
each dimension, for example:
81


var
  a: array[,] of integer;
  b: array[,,] of integer;
begin
     SetLength(a, 4,5);
     SetLength(b, 3,5,7);
end.
The Copy function can be used to make a copy of an entire array. You cannot use Copy to duplicate only a portion,
however.
You cannot pass a dynamically allocated rectangular array to the Low or High functions. Attempting to do so will
generate a compile-time error.
Records
A record (analogous to a structure in some languages) represents a heterogeneous set of elements. Each element
is called a field; the declaration of a record type specifies a name and type for each field. The syntax of a record type
declaration is
type recordTypeName = record
       fieldList1: type1;
        ...
       fieldListn: typen;
     end
where recordTypeName is a valid identifier, each type denotes a type, and each fieldList is a valid identifier or a
comma-delimited list of identifiers. The final semicolon is optional.
For example, the following declaration creates a record type called 
TDateRec
.
type
  TDateRec = record
    Year: Integer;
    Month: (Jan, Feb, Mar, Apr, May, Jun,
            Jul, Aug, Sep, Oct, Nov, Dec);
    Day: 1..31;
  end;
Each 
TDateRec
 contains three fields: an integer value called 
Year
, a value of an enumerated type called 
Month
,
and another integer between 1 and 31 called 
Day
. The identifiers 
Year

Month
, and 
Day
 are the field designators
for 
TDateRec
, and they behave like variables. The 
TDateRec
 type declaration, however, does not allocate any
memory for the 
Year

Month
, and 
Day
 fields; memory is allocated when you instantiate the record, like this:
var Record1, Record2: TDateRec;
This variable declaration creates two instances of 
TDateRec
, called 
Record1
 and 
Record2
.
You can access the fields of a record by qualifying the field designators with the record's name:
Record1.Year := 1904;
82


Record1.Month := Jun;
Record1.Day := 16;
Or use a with statement:
with Record1 do
 begin
    Year := 1904;
    Month := Jun;
    Day := 16;
 end;
You can now copy the values of 
Record1
's fields to 
Record2
:
Record2 := Record1;
Because the scope of a field designator is limited to the record in which it occurs, you don't have to worry about
naming conflicts between field designators and other variables.
Instead of defining record types, you can use the 
record ...
 construction directly in variable declarations:
var S: record
      Name: string;
      Age: Integer;
    end;
However, a declaration like this largely defeats the purpose of records, which is to avoid repetitive coding of similar
groups of variables. Moreover, separately declared records of this kind will not be assignment-compatible, even if
their structures are identical.

Yüklə 0,84 Mb.

Dostları ilə paylaş:
1   ...   89   90   91   92   93   94   95   96   ...   294




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin