2.2 Entities and Declared Types

 An entity is a component of a VBA Environment that can be accessed by name or index, according to the resolution rules for simple name expressions, index expressions and member access expressions. Entities include projects, procedural modules, types (class modules, UDTs, Enums or built-in types), properties, functions, subroutines, events, variables, literals, constants and conditional constants.

For many kinds of entities, it is only valid to reference an entity that is accessible from the current context. Entities whose accessibility can vary have their accessibility levels defined in later sections specific to these entities.

Most entities have an associated a declared type. A declared type is a restriction on the possible data values (section 2.1) that a variable (section 2.3) can contain. Declared types are also used to restrict the possible data values that can be associated with other language entities. Generally declared types restricts the data value according to the data value’s value type (section 2.1).

The following table defines the VBA declared types. Every variable within a VBA Environment has one of these declared types and is limited to only containing data values that conform to the declared type’s data value restrictions.

Declared Type

Data Value Restrictions

Boolean, Byte, Currency,

Date, Double, Integer, Long,

LongLong, Object, Single, or

String

Only data values whose value type has the same name as the declared type.

Note the following:

§ Decimal is not a valid declared type.

§ LongLong is a valid declared type only on VBA implementations that support 64-bit arithmetic.

Variant

No restrictions, generally any data value with any value type. However, in some contexts Variant declared types are explicitly limited to a subset of possible data values and value types.

String*n, where n is an integer between 1 and 65,526

Only data values whose value type is String and whose character length is exactly n.

Fixed-size array whose declared element type is one of Boolean, Byte, Currency, Date, Double, Integer, Long,

LongLong, Object, Single, String, String*n, a specific class name, or the name of a UDT.

Only homogeneous array data values that conform to the following restrictions:

§ The value type of every element (section 2.1.1) data value is the same as the variable’s declared element type. If the variable’s element declared type is a specific class name then every element of the data value MUST be either the object reference Nothing or a data value whose value type is object reference and which identifies either an object that is an instance (section 2.5) of the named element class or an object that conforms (section 2.5) to the public interface (section 2.5) of the named class.

§ The number of dimensions of the data value is the same as the variable’s number of dimensions.

§ The upper and lower bounds (section 2.1) are the same for each dimension of the data value and the variable.

Fixed-size array whose declared element type is

Variant

Only data values whose value type is Array and that conform to the following restrictions:

§ The number of dimensions of the data value is the same as the variable’s number of dimensions.

§ The upper and lower bounds are the same for each dimension of the data value and the variable.

Resizable array whose declared element type is one Boolean, Byte, Currency,

Date, Double, Integer, Long,

LongLong, Object, Single, String, String*n, a specific

class name, or the name of a

UDT

Only homogeneous array data values where the value type of every element data value is the same as the variable’s declared element type. If the variable’s element declared type is a specific class name then every element of the data value MUST be either the object reference Nothing or a data value whose value type is object reference and which identifies either an object that is an instance of the named element class or an object that conforms to the public interface of the named class.

 

Resizable array whose declared element type is Variant

Only data values whose value type is Array.

Specific class name

Only the object reference data value Nothing and those data values whose value type is object reference and which identify either an object that is an instance of the named class or an object that conforms to the public interface of the named class.

Specific UDT name

Only data values whose value type is the specific named UDT.

 

As with value types, there is no Enum-specific declared type. Instead, declarations using an Enum type are considered to have a declared type of Long. Note that there are no extra data value restrictions on such Enum declarations, which might contain any Long data value, not just those present as Enum members within the specified Enum type.

An implementation-defined LongPtr type alias is also defined, mapping to the underlying declared type the implementation will use to hold pointer or handle values. 32-bit implementations SHOULD map LongPtr to Long, and 64-bit implementations SHOULD map LongPtr to LongLong, although implementations MAY map LongPtr to an implementation-defined pointer type. The LongPtr type alias is valid anywhere its underlying declared type is valid.

Every declared type except for array and UDT declared types are scalar declared types.