属性过程是一系列的 Visual Basic 语句,它允许程序员去创建并操作自定义的属性。属性过程可以用来为窗体,标准模块,以及类模块创建只读属性。可被用来在代码中代替 Public 变量,当设置属性值时上述动作应被执行。
与 Public 变量不同,在对象浏览器中属性过程会有一些帮助字符串指定给自定义的属性。
当创建一个属性过程时,它会变成此过程所包含的模块的一个属性。Visual Basic 提供下列三种类型的 Property 过程:
Property Let 用来设置属性值的过程。
Property Get 用来返回属性值的过程。
Property Set 用来设置对对象引用的过程。
声明属性过程的语法如下所示:
[Public | Private] [Static] Property {Get | Let | Set}
propertyname_ [(arguments)] [As type]
statements
End Property
属性过程通常是成对使用的:Property Let 与 Property Get 一组,而 Property Set 与 Property Get 一组。单独声明一个 Property Get 过程就像声明只读属性。三个 Property 过程一起使用时,只有对 Variant 变量有用,因为只有 Variant 才能包含一个对象或其他数据类型的信息。Property Set 本意是使用在对象上;而 Property Let 则不是。
在 Property 过程声明中所需要的参数如下:
Property Get Property Get propname(1, ..., n) As type
Property Let Property Let propname(1, ..., n, n+1)
Property Set Property Set propname(1, ..., n, n+1)
在具有相同名称属性过程中,从第一个到最后一个参数(1, ..., n)都必须共享相同的名称与数据类型。
Property Get 过程声明时所需的参数比相关的 Property Let 以及 Property Set 声明少一个。Property Get 过程的数据类型必须与相关的 Property Let 以及 Property Set 声明中的最后 (n+1)个参数的类型相同。例如,如果声明下列的 Property Let 过程,则 Property Get 声明所使用参数的名称与数据类型必须同 Property Let 过程中所用的一样。
Property Let Names(intX As Integer, intY As Integer,
varZ As Variant)
' 执行语句.
End Property
Property Get Names(intX As Integer, intY As Integer)
As Variant
' 执行语句.
End Property
在 Property Set 声明中,最后一个参数的数据类型必须是对象类型或是 Variant。