VBA语言规范之声明类型(Declared Type in VBA Language Specification)

VBA编程时,需要什么数据类型一般先声明相应类型的变量。这样,有助于理清编程思路,防止出现数据类型不兼容、丢失精度、浪费存储空间等问题。声明变量类型后,变量所能赋得的值也就相应的收到了限制,具体如下所述:

注:考虑到翻译起来费事费力而且怕翻译的不准确误导别人,所以保留英文原文,以备需要时查阅。

声明类型
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,535

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.



猜你喜欢

转载自blog.csdn.net/hpdlzu80100/article/details/80664299
vba