注册 登录
Office中国论坛/Access中国论坛 返回首页

ganlinlao的个人空间 http://www.office-cn.net/?230471 [收藏] [复制] [分享] [RSS]

日志

VBA Attributes 属性详解

已有 167 次阅读2024-1-6 18:26 |个人分类:vb入门

          VBA  Attributes 属性详解

         VBA 代码模块包含可用于设置模块及其成员某些属性的属性。在 VBE 编辑器中看不到Attributes属性。若要查看代码模块的属性,请导出该模块并在文本编辑器中打开该模块。可以添加或更改属性,然后将文件导入回 VBA 项目,以让它们生效。

   Attributes属性可用于设置模块或类的名称、向代码资源添加提示说明、为类提供默认全局实例、设置类的默认成员以及设置用于 For Each 循环类的枚举器。某些特定属性可以在导出的文件中看到,但在 VBA 中不起作用,而是用于VB6。通常,在VBA中没有必要也不建议设置专用于VB6的属性。 

VBA 中可用的属性

属性

描述

VB_Name

用于设置模块或类的名称。

VB_[Var]Description

用于设置模块、类、过程和变量的描述文本。

VB_PredeclaredId

设置为 True 将在应用程序启动时创建类的默认全局实例。

VB_Exposed

设置为 True 可使类类型在 VBA 项目外部可用。类类型的对象可以存在于 VBA 项目外部,但不能在 VBA 项目外部实例化。从公开的类创建的实例必须从 VBA 项目中定义的工厂函数传递。

VB_[Var]UserMemId

设置为 0 将使该成员成为默认成员。设置为 -4 会使该成员成为要与 For Each 循环一起使用的类的枚举器。

 VBA 中不可用,专用于VB6的属性

属性

描述

VB_GlobalNameSpace

* VBA 中不起作用。使类的作用域全局化,因此不需要使用类的名称来引用类成员。

VB_Creatable

* VBA 中不起作用。设置为 True 允许在定义类的 VBA 项目外部实例化类。

VB_MemberFlags

* VBA 中不起作用。设置为“40”可隐藏成员。

 

VB_Name 

   VB_Name 属性可用于设置模块或类的名称。还可以在 Visual Basic 编辑器的“属性”窗口中设置此属性。 

VERSION 1.0 CLASS

BEGIN

  MultiUse = -1  'True

END

Attribute VB_Name = "clsExample"

Attribute VB_GlobalNameSpace = False

Attribute VB_Creatable = False

Attribute VB_PredeclaredId = False

Attribute VB_Exposed = False

Option Explicit

Public Message As String

Public Sub Example()

    Debug.Print Message

End Sub

 

 

VB_[Var]Description    说明 

      VB_[Var]Description 属性可以设置为模块、类、过程和变量的描述文本。声明模块的属性时,请使用“VB_Description”格式。声明过程的属性时,请在过程中使用“ProcedureName.VB_Description”形式。声明变量的属性时,请使用“VariableName.VB_VarDescription”格式。 

VERSION 1.0 CLASS

BEGIN

  MultiUse = -1  'True

END

Attribute VB_Name = "clsExample"

Attribute VB_Description = "This is a description of a class"

Attribute VB_GlobalNameSpace = False

Attribute VB_Creatable = False

Attribute VB_PredeclaredId = False

Attribute VB_Exposed = False

Option Explicit

Public Message As String

Attribute  Message.VB_VarDescription = "This is a description of a member variable"

Public Sub Example()

Attribute  Example.VB_Description = "This is a description of a procedure"

    Debug.Print Message

End Sub

 

 

 

VB_PredeclaredId

VB_PredecalredId 属性可用于创建类的默认全局实例。 将此属性设置为 True 将导致在应用程序启动时创建全局实例。

VB_PredeclaredId的特性,是VBA中最接近模拟类的静态方法。

我们可以利用 类的静态方法,做不少有用的事情。

Option Explicit

Public Sub Example()

    Dim TheSum As Double   

    TheSum = MyMathFunctions.Add2Numbers(3.5, 3.5)   

    Debug.Print TheSum

End Sub

VERSION 1.0 CLASS

BEGIN

  MultiUse = -1  'True

END

Attribute VB_Name = "MyMathFunctions"

Attribute VB_GlobalNameSpace = False

Attribute VB_Creatable = False

Attribute VB_PredeclaredId = True

Attribute VB_Exposed = False

Option Explicit

Public Function Add2Numbers(Num1 As Double, Num2 As Double) As Double

    Add2Numbers = Num1 + Num2

End Function

 

VB_Exposed

VB_Exposed 属性用于指定类的可访问性。设置为 True 时,此属性使类类型对 VBA 项目外部可见。这与将类的 Instancing 属性设置为 PublicNotCreatable 具有相同的效果。默认情况下,此属性设置为 False,这将使 Instancing 属性成为 PrivatePublicNotCreateable 类允许在定义类类型的变量的 VBA 项目外部声明变量,但不能在定义类类型的对象的 VBA 项目外部实例化这些变量。VBA 项目中的公共工厂函数可用于实例化类实例,该类实例可以从 VBA 项目外部调用。

 

警告!即使将 VB_Expose 设置为 True,其他项目仍然无法创建类或用户窗体的新实例 ,它们只能使用类或用户窗体所属项目创建的现有实例。因此需要在 类或用户窗体所属项目的标准模块中建立一个公共函数,方便“其他项目”新建类的实例,例如

Public Function GetNewClass1() As Class1

    Set GetNewClass1 = New Class1

End Function

 VB_[Var]UserMemId

VB_[Var]UserMemId 属性可用于设置类的默认成员,并为要与 For Each 循环一起使用的类设置枚举器。 

默认成员

若要使成员成为类的默认成员,请使用“MemberName.[Var]UserMemId“并将属性设置为 0。成员变量或过程可以是默认成员。最常见的做法是将 Property 作为默认成员。 

Option Explicit

Public Sub Example()

    Dim E As clsExample

    Set E = New clsExample

    'The Message property is the default member

    E = "Hello, World!"

    Debug.Print E

End Sub

 

VERSION 1.0 CLASS

BEGIN

  MultiUse = -1  'True

END

Attribute VB_Name = "clsExample"

Attribute VB_Description = "This is a description of a class"

Attribute VB_GlobalNameSpace = False

Attribute VB_Creatable = False

Attribute VB_PredeclaredId = False

Attribute VB_Exposed = False

Option Explicit

Private pMessage As String

Attribute pMessage.VB_VarDescription = "Private Message Variable"

Public Property Get Message() As String

Attribute Message.VB_Description = "This is a description of a member variable"

Attribute Message.VB_UserMemId = 0

    Message = pMessage

End Property

Public Property Let Message(RHS As String)

Attribute Message.VB_Description = "This is a description of a member variable"

Attribute Message.VB_UserMemId = 0

    pMessage = RHS

End Property

Public Sub Example()

Attribute Example.VB_Description = "This is a description of a procedure"

    Debug.Print Message

End Sub

Enumerator枚举器 

若要从过程返回枚举器,请在该过程将 Collection 对象的 [_NewEnum] 隐藏属性作为 IUknown 接口类型返回。使用格式“PropertyName.VB_UserMemId”并将属性设置为 -4 以指示属性是枚举器。 

Option Explicit

Public Sub Example()

    Dim E As clsExample

    Set E = New clsExample   

    Set E.Coll = New Collection

    E.Coll.Add 1

    E.Coll.Add 2

    E.Coll.Add 3   

    Dim i As Variant

    For Each i In E

        Debug.Print i

    Next i

End Sub

 

VERSION 1.0 CLASS

BEGIN

  MultiUse = -1  'True

END

Attribute VB_Name = "clsExample"

Attribute VB_Description = "This is a description of a class"

Attribute VB_GlobalNameSpace = False

Attribute VB_Creatable = False

Attribute VB_PredeclaredId = False

Attribute VB_Exposed = False

Option Explicit

Public Coll As Collection

Attribute Coll.VB_VarDescription = "This is a description of a member variable"

Public Property Get NewEnum() As IUnknown

Attribute NewEnum.VB_UserMemId = -4

         Set NewEnum = Coll.[_NewEnum]

End Property

Public Sub Example()

Attribute Example.VB_Description = "This is a description of a procedure"

    Debug.Print Message

End Sub

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-4-28 16:02 , Processed in 0.097402 second(s), 17 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

返回顶部