IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

October Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi for W...   Print This     
  From the May 2010 Issue of Prestwood eMag
 
Delphi for Win32:
Delphi Array (x=Array[0..3] of string;)
 
Posted 14 years ago on 3/30/2010
Delphi Code Snippet:
 A flashcard from our Delphi Flashcards Library
 A code snippet from our Delphi Code Snippets Page
 Tags: Delphi , Array

KB102139

Languages Focus: Array

A data structure in which individual values (called elements or items) may be located by reference to one or more integer index variables, the number of such indices being the number of dimensions in the array.

Arrays can start with an index value of 0 or 1, sometimes referred to as 0 based or 1 based.

Delphi Array

Delphi supports both static and dynamic arrays as well as single and multi dimensional arrays.

Syntax Example:
var
  MyArray: array[0..3] of string;
  i: Integer;
begin
  MyArray[0] := 'Mike';
  MyArray[1] := 'Lisa';
  MyArray[2] := 'Felicia';
  MyArray[3] := 'Nathan';
  
  for i := 0 to High(MyArray) do
    ShowMessage(MyArray[i]);
end;

0-Based, 1-Based: A Choice

Because you can specify both he upper and lower index values when you declare a static array, you can specify 0 or 1 as the base index value. For example, you can rewrite the above code as:

var
  MyArray: array[1..4] of string;
  i: Integer;
begin
  MyArray[1] := 'Mike';
  MyArray[2] := 'Lisa';
  MyArray[3] := 'Felicia';
  MyArray[4] := 'Nathan';
  
  for i := Low(MyArray) to High(MyArray) do
    ShowMessage(MyArray[i]);
end;

Note the use of Low() in place of the base index value in the for loop.

In fact, you could specify any range you wish,

MyArray: array[6..9] of string;

or even:

MyArray: array[-1..2] of string;

You can also use an ordinal type such as Word or Byte directlry. For exaple, because a Byte is equivalent to 0..255 the following:

MyArray: array[Byte] of string;

is equivalent to:

MyArray: array[0..255] of string;

Initialize Global Arrays

You can initialize the values of a global array when you declare it, but not a local array. For example, the following code snippet is in the interface section of a unit.

var
  Form2: TForm2;
  MyArray2: array[0..1] of string = ('Mike', 'Lisa');

 


Comments

1 Comments.
Share a thought or comment...
Comment 1 of 2

Excellent.

I love arrays. I use them to hold data (array of a record) for data in/out of the application. I use these in lieu of data-aware controls. My options are much greater if I do not use data-aware controls.

Here is how I implement dynamic arrays:

1) declare a record

TrecAddress = record
  Name:   string;
  Address: string;
 City: string;
 State: string;
 Zip: string;
end;

2) my dynamic array defined:

TaryAddressRec = array of TrecAddress;

3) gather some data in a query, then iterate through the query to dimension the array size

var
nCount: integer;
aryWork: TaryAddressRec;
begin
nCount := 0;
while (not qryWork.Eof) do
begin
  inc(nCount);
  qryWork.Next;
end;

// sets the size of the array to the number of rows found
// since I deal with multipe DB types, Access, Oracle, MySQL, Elevate, I cannot rely on recordcount.

SetLength(aryWork,nCount);
nCount := -1;
while (not qryWork.Eof) do
begin
 // may wish to initialize the array record here
 inc(nCount);
 // load the array
 aryWork[nCount].Name := qryWork.FieldByName('Name').AsString;
 aryWork[nCount].Address := qryWork.FieldByName(Address').AsString;
...
qryWork.Next;
end;
// close connection
end;

Anyway, I like arrays - they are Cool

Posted 14 years ago

Comment 2 of 2

A simple array with more explaination is now launched. More information you can check when you start to read assignment help, I also want to invite you to read these array which you can see in the data.

Posted 54 months ago
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1116A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile


Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

Beginner

1 Beginner Level Question

Question #1: Multiple Choice

Which is the correct syntax for declaring an array and setting two values of the array?

Answer:
1. 
Dim MyArray As Variant
MyArray = Array("Mike", "Lisa")
2. 
var
  MyArray: array[0..3] of string;
begin
  MyArray[0] := 'Mike';
  MyArray[1] := 'Lisa';
end;
3. 
var
  MyArray Array[2] String
endVar
MyArray[1] = "Mike"
MyArray[2] = "Lisa"
4. 
var
  MyArray: array[0..3] of string;
begin
  MyArray(0) = 'Mike';
  MyArray(1) = 'Lisa';
end;
5. 
var
  MyArray array(0..3) : string;
begin
  MyArray[0] := 'Mike';
  MyArray[1] := 'Lisa';
end;

 KB Article #102139 Counter
23848
Since 3/30/2010
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]