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.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值