编写属性过程

属性过程是一系列 Visual Basic 语句 ,允许程序员创建和操作自定义属性。

  • Property 过程可用于为窗体标准模块类模块创建只读属性。

  • 设置属性值时,应在必须执行的代码中使用 Property 过程替代 Public 变量。

  • 公共 变量不同,属性过程可以在 对象浏览器中将帮助字符串分配给它们。

创建属性过程时,它将成为包含该过程的 模块 的属性。 Visual Basic 提供以下三种类型的属性过程。

Procedure Description
Property Let 一个设置属性值的过程。
Property Get 一个返回属性值的过程。
Property Set 一个设置对对象的引用的过程。

用于声明属性过程的语法如下所示。

[ 公共 | 专用 ][ 静态 ] Property { Get | Let | Set } propertyname [ ( arguments ) ] [ Astype ] 语句End Property

属性过程通常成对使用: Property Let with Property GetProperty Set with Property Get。 单独声明 Property Get 过程类似于声明只读属性。 将这三个属性过程类型一起使用仅对 Variant 变量有用,因为只有 Variant 可以包含对象或其他数据类型信息。 Property Set 可用于对象,而 Property Let 不可以。

下表显示了属性过程声明中所需的参数。

Procedure 声明语法
Property Get 属性获取propname (1, ..., n) As类型
Property Let 属性 Letpropname (1, ...,,,, nn +1)
Property Set 属性集propname (1、...、 nn +1)

第一个参数 (最后一个参数 (1, ..., n) 必须在所有属性过程中使用相同的名称共享相同的名称和数据类型。

Property Get 过程声明较相关的 Property LetProperty Set 声明少一个参数。 Property Get 过程的数据类型必须与相关 Property Let 和 PropertySet 声明中最后一个参数 (n +1) 的数据类型相同。 例如,如果您声明以下 Property Let 过程,则 Property Get 声明必须使用与 Property Let 过程中的参数具有相同的名称和数据类型的参数。

Property Let Names(intX As Integer, intY As Integer, varZ As Variant) 
 ' Statement here. 
End Property 
 
Property Get Names(intX As Integer, intY As Integer) As Variant 
 ' Statement here. 
End Property 

Property Set 声明中的最后一个参数的数据类型必须为对象类型Variant

另请参阅

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。