我创建了一个新的People.cls文件,其中包含以下代码:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = “People”
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
Private objPeople As Collection
Private Sub Class_Initialize()
Set objPeople = New Collection
End Sub
但是,当我从VBA编辑器导入cls文件时,它似乎不起作用。我做错了吗?
我想添加一个Custom Collection Class作为内置Collection类的包装器。
据我所知,如果没有附加属性修饰符,则Item属性将不是默认属性,并且您将无法在Collection上使用For Each / Next。
答案 0 :(得分:1)
Attribute VB_Name = “People”
将导致IDE因“”
而无法为您的类命名。将它们替换为常规双引号""
,您就可以了。
答案 1 :(得分:0)
我建立了一个"通用模板" for CollectionWrapper' s(* .cls)
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "CollectionWrapper"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Description = "Extended Collection"
Option Explicit
Option Base 0
{your Public Const}
{您的公共变量}
{your Private Const}
Private Locals As Locals
Private Type Locals
Collection As New Collection
End Type
{您的私人变量私人变量}
{您的私人类型}
{您的私人变量}
Property Get Item(Index) As Variant
Attribute Item.VB_Description = "Default Property"
Attribute Item.VB_UserMemId = 0
SetOrLet Target:=Item, Source:=Locals.Collection(Index)
End Property
Property Get NewEnum() As Variant
Attribute NewEnum.VB_Description = "Enumerator Property"
Attribute NewEnum.VB_UserMemId = -4
Attribute NewEnum.VB_MemberFlags = "40"
' Gets an enumerator that iterates through the List.
' http://stackoverflow.com/documentation/vba/5321/attributes#t=201701130621511275744
' http://stackoverflow.com/questions/26721017/unable-to-create-new-enum-in-vba
' https://msdn.microsoft.com/en-us/library/aa262338(v=vs.60).aspx
Set NewEnum = Locals.Collection.[_NewEnum]
End Property
Property Get ClassName() As String
ClassName = TypeName(Me)
End Property
Private Function SetOrLet(ByRef Target As Variant, Source As Variant) As Variant
If IsObject(Source) Then
Set Target = Source
Set SetOrLet = Target
Else
Let Target = Source
Let SetOrLet = Target
End If
End Function
{您的代码在这里}