从项目集合中调用项目
Call an Item from a Collection of Items
我有一个 class“货币”和一个 class“货币”,它是“货币”class 对象实例的集合。我认为这类似于 Excel 的 Worksheet
class,它是 Sheets
集合的成员。我可以通过索引或“键”来称呼任何成员,例如 Sheets(1)
或 `Sheets("Sheet1").
这是我的“货币”class 模块中的代码。它是此处使用的缩写,可能不是 运行,这不是问题。
Private Sub Class_Initialize()
' Class "Currencies"
Dim R As Long
Set AllCcys = New Collection
Arr = .Range("Currencies").Value
For R = 1 To UBound(Arr)
Set Ccy = Me.Add(Arr(R, 1))
Next R
End Sub
Public Function Add(ByVal Key As String) As cCcy
Dim Fun As cCcy
Set Fun = New cCcy
AllCcys.Add Fun, Key
Fun.Key = Key
Set Add = Fun
End Function
Public Property Get Item(Key As Variant) As cCcy
Set Item = AllCcys(Key)
End Property
通过我的设置,我可以使用我认为复杂的 Currencies.Item("USD").Rate
或 Currencies.Item(1).Rate
等语法访问任何“货币”对象。我想使用 Currencies("USD").Rate
模拟访问 Excel 的 Sheets
集合时所做的事情。
我怎样才能做到这一点?
根据http://www.cpearson.com/excel/DefaultMember.aspx,您可以通过以下步骤将自定义class中的任何过程指定为默认成员:
- 导出
Currencies
class模块
- 在记事本中打开导出的模块
- 在您的
Item
属性 中添加 Attribute Value.VB_UserMemId = 0
,如下所示:
Public Property Get Item(Key As Variant) As cCcy
Attribute Value.VB_UserMemId = 0
Set Item = AllCcys(Key)
End Property
- 保存并导入回您的文件。
注意:您只能将 1 个过程作为默认成员,并且您不会在 VBE 中看到行 Attribute Value.VB_UserMemId = 0
。
我有一个 class“货币”和一个 class“货币”,它是“货币”class 对象实例的集合。我认为这类似于 Excel 的 Worksheet
class,它是 Sheets
集合的成员。我可以通过索引或“键”来称呼任何成员,例如 Sheets(1)
或 `Sheets("Sheet1").
这是我的“货币”class 模块中的代码。它是此处使用的缩写,可能不是 运行,这不是问题。
Private Sub Class_Initialize()
' Class "Currencies"
Dim R As Long
Set AllCcys = New Collection
Arr = .Range("Currencies").Value
For R = 1 To UBound(Arr)
Set Ccy = Me.Add(Arr(R, 1))
Next R
End Sub
Public Function Add(ByVal Key As String) As cCcy
Dim Fun As cCcy
Set Fun = New cCcy
AllCcys.Add Fun, Key
Fun.Key = Key
Set Add = Fun
End Function
Public Property Get Item(Key As Variant) As cCcy
Set Item = AllCcys(Key)
End Property
通过我的设置,我可以使用我认为复杂的 Currencies.Item("USD").Rate
或 Currencies.Item(1).Rate
等语法访问任何“货币”对象。我想使用 Currencies("USD").Rate
模拟访问 Excel 的 Sheets
集合时所做的事情。
我怎样才能做到这一点?
根据http://www.cpearson.com/excel/DefaultMember.aspx,您可以通过以下步骤将自定义class中的任何过程指定为默认成员:
- 导出
Currencies
class模块 - 在记事本中打开导出的模块
- 在您的
Item
属性 中添加Attribute Value.VB_UserMemId = 0
,如下所示:
Public Property Get Item(Key As Variant) As cCcy
Attribute Value.VB_UserMemId = 0
Set Item = AllCcys(Key)
End Property
- 保存并导入回您的文件。
注意:您只能将 1 个过程作为默认成员,并且您不会在 VBE 中看到行 Attribute Value.VB_UserMemId = 0
。