Array Data Types in Visual Basic

There is no single data type for all arrays. Instead, the data type of an array is determined by the following factors:

  • The fact of being an array

  • The rank (number of dimensions)

  • The data type of the array's elements

Therefore, two array variables are considered to be of the same data type only when they have the same rank and their elements have the same data type.

Note that the lengths of the dimensions do not influence the array data type.

The following example declares four array variables, which have various data types.

Dim firstArray(12, 8) As UInteger
Dim secondArray(12, 8, 3) As UInteger
Dim thirdArray(12, 8) As String
Dim fourthArray(5, 20) As UInteger

Following the execution of the preceding statements, the following data type relationships apply to the array variables:

  • Variables firstArray and secondArray are not of the same data type because they have different ranks.

  • Variables firstArray and thirdArray are not of the same data type because they have different element data types.

  • Variables firstArray and fourthArray are of the same data type, and you can assign one to the other.

Jagged Array Data Types

Arrays of arrays, that is, arrays that contain other arrays as elements, are also known as jagged arrays because the lengths of the element arrays are not necessarily equal. The following example declares two jagged array variables, which have different data types.

Dim twoDimOfOneDim(,)() As Integer
Dim oneDimOfTwoDim()(,) As Integer

The array in twoDimOfOneDim is two-dimensional, and its element data type is Integer(), or one-dimensional Integer arrays. The array in oneDimOfTwoDim is one-dimensional, and its element data type is Integer(,), or two-dimensional Integer arrays.

Community Additions

ADD
Show: