IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
   ► KBTo/From GuidesReferenceData Structures  Print This     

Array (Cross Ref > Data Structures)

Array

Languages Focus

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.

Access VBA:   x = Array()

Arrays in Access VBA use a 0-based indice. Use UBound to get the number of elements. UBound returns -1 if the array has no elements, 0 if it has 1, 1 if it has 2, etc.

Syntax Example:
Dim MyArray As Variant
Dim i As Integer
 
MyArray = Array("Mike", "Lisa", "Felicia", "Nathan")
 
If UBound(MyArray) > -1 Then
  For i = 0 To UBound(MyArray)
    MsgBox (MyArray(i))
  Next
End If


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.

Intermediate

1 Intermediate Level Question

Question #1: True or False?

Arrays in Access VBA are 0 based.

Answer:
  • True
  • False
  • More Info

     

    ASP Classic:   x = Array()

    Arrays in ASP Classic use a 0-based indice.

    Use UBound to get the number of elements. UBound returns -1 if the array has no elements, 0 if it has 1, 1 if it has 2, etc.

    Syntax Example:
    Dim MyArray, i
     
    MyArray = Array("Mike", "Lisa", "Felicia", "Nathan")
     
    If UBound(MyArray) > -1 Then
      For i = 0 to UBound(MyArray)
        Response.Write MyArray(i)
      Next
    End If

    Dynamically sized Arrays

    Dim myArray()
    Dim ArrayItem
     
    ReDim Preserve myArray(0)     '>>>0 based array.
    myArray(0) = "Nathan"
     
    ReDim Preserve myArray(1)
    myArray(1) = "Felly"
     
    ''Loop thru array.
    ''''''''''''''''''
    For Each ArrayItem In myArray

     Response.Write ArrayItem

    Next

    Looping

    In addition to using a for each loop above, you can use a for loop as follows:

    Dim myArray
    Dim ArrayCounter
    '...populate array here...
    For ArrayCounter = 0 to UBound(myArray)
     Response.Write myArray(ArrayCounter)
    Next



    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.

    Intermediate

    1 Intermediate Level Question

    Question #1: Yes or No?

    Are arrays in ASP Classic zero-based?

    Answer:
  • Yes
  • No
  • More Info

     

    Corel Paradox:   Array[] type

    Arrays in ObjectPAL use a 1-based subscript (also known as an index). Use [] with a subscript to refer to an element (i.e. MyArray[1], MyArray[2]).

    The number of elements for a given array in ObjectPAL are either fixed or resizeable. Specify the size when you declare it for fixed. Use size() to get the number of elements, size() returns 0 if the array has no elements. Use setSize(), grow(), addLast(), etc. to add elements to an array.

    Syntax Example:

    var
      MyArray Array[4] String ;Fixed size array.
      i LongInt
    endVar

    MyArray[1] = "Mike"
    MyArray[2] = "Lisa"
    MyArray[3] = "Felicia"
    MyArray[4] = "Nathan"

    if MyArray.size() > 0 then
      for i from 1 to MyArray.size()
        msgInfo("", MyArray[i])
      endFor
    endIf

    The above example, is an example of a fixed size array in which you specified the size of the array when declared:

    MyArray Array[4] String

    Resizeable Arrays

    With a resizeable array, you can set an initial size of the array and grow it as needed:

    var
      MyArray Array[] String ;Resizeable array.
      i LongInt
    endVar
      
    MyArray.setSize(3)       ;Set initial size.
      
    MyArray[1] = "Mike"
    MyArray[2] = "Lisa"
    MyArray[3] = "Felicia"
      
    MyArray.setSize(4)      ;Reset size (truncates if you set smaller.)
    MyArray[4] = "Nathan"
      
    MyArray.Grow(1)         ;Add to the array with grow.
    MyArray[5] = "Ingo"

    if MyArray.size() > 0 then
      for i from 1 to MyArray.size()
        msgInfo("", MyArray[i])
      endFor
    endIf



    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.

    Intermediate

    1 Intermediate Level Question

    Question #1: True or False?

    An ObjectPAL array is 0-based, meaning, the first element has an index of 0.

    Answer:
  • True
  • False
  • More Info

    Code:  ObjectPAL Array (Array[] type)

    Delphi:   x=Array[0..3] of string;

    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');

     



    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[2] String
    endVar
    MyArray[1] = "Mike"
    MyArray[2] = "Lisa"
    3. 
    var
      MyArray: array[0..3] of string;
    begin
      MyArray[0] := 'Mike';
      MyArray[1] := 'Lisa';
    end;
    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;

    More Info

    VB Classic:   x = Array()

    Arrays in VB Classic use a 0-based indice. Use UBound to get the number of elements. UBound returns -1 if the array has no elements, 0 if it has 1, 1 if it has 2, etc.

    Syntax Example:
    Dim MyArray As Variant
    Dim i As Integer
     
    MyArray = Array("Mike", "Lisa", "Felicia", "Nathan")
     
    If UBound(MyArray) > -1 Then
      For i = 0 To UBound(MyArray)
        MsgBox (MyArray(i))
      Next
    End If


    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.

    Intermediate

    1 Intermediate Level Question

    Question #1: True or False?

    A VB classic array is 0-based, meaning the first index is 0.

    Answer:
  • True
  • False
  • More Info

     




    Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
    www.prestwood.com


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