Language > Static Methods and Attributes > Variable Attributes

# Variable Attributes

All IDL variables have special attributes that return information about the variable. These attributes are equivalent to calling the N_ELEMENTS, SIZE, or TYPENAME functions.

The following variable attributes are available:

Attribute | Definition
--- | ---
DIM | An array giving the dimensions (0 for scalars).
LENGTH | The number of elements in the variable.
NDIM | An integer giving the number of dimensions (0 for scalars).
TNAME | A string giving the raw IDL type name. For structures this returns "STRUCT", while for objects this returns "OBJREF".
TYPECODE | An integer giving the IDL type code.
TYPENAME | A string giving the IDL type name. For structures and objects this returns the actual structure or class name.

Note: For structures and objects, if the structure or object contains a field with the same name as an attribute, then IDL will correctly retrieve the structure field and you will not be able to access the attribute.

Note: For most variables, the TNAME and TYPENAME attributes will return the same result. For structures and objects the TYPENAME will return the actual structure or class name, just like the TYPENAME function.

## Examples

#### Scalars

First print the attributes for a scalar variable:

var = "hello"

PRINT, var.length

PRINT, var.typename

PRINT, var.typecode

PRINT, var.ndim

PRINT, var.dim

IDL prints:

1

STRING

7

0

0

#### Arrays

Next, print the attributes for an array:

var = RANDOMU(seed, 100, 50, 3)

PRINT, var.length

PRINT, var.typename

PRINT, var.typecode

PRINT, var.ndim

PRINT, var.dim

IDL prints:

15000

FLOAT

4

3

100 50 3

#### Structure Fields

The attributes can also be used on fields within structures. Here we use the LENGTH attribute as the loop limit:

var = {mystruct, field1: {substruct, subfield: LONARR(1000)}}

for i=0, var.field1.subfield.LENGTH-1 do var.field1.subfield[i]=i

; Use our static TOTAL method

PRINT, var.field1.subfield.total()

IDL prints:

499500.

#### Objects

For objects that inherit from IDL_Object, the LENGTH, NDIM, and DIM attributes will be taken from the overloaded ::Size method. For example:

h = HASH('key1', 4, 'key2', 5, 'key3', 6)

PRINT, h.length, h.ndim, h.dim, h.typecode, h.tname, h.typename

IDL prints:

3

1

3

11

OBJREF

HASH

Notice that the TNAME and TYPENAME attributes are different for objects.

## Version History

8.4 | Introduced

## See Also

IDL_Variable, ISA, N_ELEMENTS, SIZE, TYPENAME