Share via


How to: Determine the Data Type of an Array

Remember that an array's data type is never the same as that of its elements. You can find out the data type of either an array or its elements in several ways.

  • You can call the Object.GetType method on the variable to receive a Type object for the run-time type of the variable. The Type object holds extensive information in its properties and methods.

  • You can pass the variable to the TypeName Function (Visual Basic) to receive a String containing the name of run-time type.

  • You can pass the variable to the VarType Function (Visual Basic) to receive a VariantType value representing the type classification of the variable.

To determine the data type of an array

  • Call TypeName on the array name. Do not follow the array name with parentheses, because you are requesting the type of the array itself.

    Dim thisTwoDimArray(,) As Integer = New Integer(9, 9) {}
    MsgBox("Type of thisTwoDimArray is " & TypeName(thisTwoDimArray))
    

    The MsgBox call displays "Type of thisTwoDimArray is Integer(,)", which shows you both the element type and the number of dimensions. It does not display the current lengths of the dimensions, because they are not part of an array's data type.

To determine the data type of an array element

  • Select an existing element and call TypeName on that element.

    Dim thisTwoDimArray(,) As Integer = New Integer(9, 9) {}
    MsgBox("Type of thisTwoDimArray(0, 0) is " & TypeName(thisTwoDimArray(0, 0)))
    

    The MsgBox call displays "Type of thisTwoDimArray(0, 0) is Integer".

    The element data type is part of the array's data type. Because of this, you cannot change the data type, even with an assignment statement or a ReDim statement.

See Also

Tasks

How to: Declare an Array Variable

How to: Create an Array

How to: Initialize an Array Variable

Troubleshooting Arrays

Concepts

Array Data Types in Visual Basic

Reference

TypeName Function (Visual Basic)

VarType Function (Visual Basic)

VariantType Enumeration

Other Resources

Arrays in Visual Basic