【问题标题】:Excel VBA - creating a collection class within a parent classExcel VBA - 在父类中创建集合类
【发布时间】:2019-05-24 09:15:00
【问题描述】:

我一直在尝试创建一个类,它是(另一个类的)对象的集合并且在父类中。我在这里查看了几个问题,但无法正常工作。因此,如果有人可以使用我的参数发布一个简短的代码,我将非常感激。

我的父类是 Sample。它应该包含一个集合 SampleFields,该集合应该包含来自类 SampleField 的对象。 SampleField 对象只有一个 Name 属性,它取自单元格 A1 到 D1。应该可以从 SampleFields 集合中添加和删除项目,并修改 SampleField 对象的 Name 属性。 SampleFields 集合在 Sample 类初始化时获取其对象。

我需要像这样访问它 - Sample.SampleFields(1).Name

我认为发布我的尝试是没有用的,但这里是:

Sub test()

Dim a As New Sample, i As Variant

a.GetFields

For Each i In a.SampleFields
    Debug.Print i.Name
Next

End Sub

示例类:

Private pFields As New SampleFields

Public Property Get SampleFields() As SampleFields
    Set SampleFields= pFields
End Property

Public Property Set SampleFields(ByVal value As SampleFields)
    Set pFields = value
End Property

Private Sub Initialize_Class()
    Set pFields = New SampleFields
End Sub


Public Sub GetFields()

Dim rngHeaders As Range, rngCell As Range
Set rngHeaders = Range("A1").CurrentRegion.Rows(1)

For Each rngCell In rngHeaders.Cells
    Dim newField As SampleField
    newField.Name = rngCell.Value2
    Me.Fields.AddNewField (newField)   'crashes here with Method or data member not found
Next

End Sub

SampleFields 类:

Private pFields As New Collection

Public Sub AddNewField(FieldName As SampleField)
    Me.AddNewField (FieldName)
End Sub

SampleField 类:

Private pName As String

Public Property Let Name(value As String)
    pName = value
End Property

Public Property Get Name() As String
    Name = pName
End Property

谢谢!

【问题讨论】:

  • 请通过提供您正在处理的代码 sn-p 并清楚地识别有问题的部分来添加更多细节。最好的问候,
  • Private pFields As New Collection 和 Private pFields As New SampleFields 似乎模棱两可。顺便说一句,如果 Collection 类已经是 Excel VBA 中的内置对象,为什么还需要它?
  • 除了内置集合的 4 个之外,我可能还需要集合的一些自定义属性/方法。

标签: excel vba


【解决方案1】:

很老的帖子,但至少让我回答一下: 在示例类中,有一个 Collection。您可以忘记 SampleFields 类,它不是必需的。

那么你只需要有一个 SampleField 类,你可以传递给这个 SampleClass 方法“AddField”,你可以使用它来增加集合的大小。

示例类应如下所示:

Private p_SampleFields as Collection
Private p_SampleField as SampleField

'Initialize this class with it's collection:
Private Sub Class_Initialize()
    Set p_SampleFields = New Collection
End Sub

'Allow for adding SampleFields:
Public Sub AddField(field as SampleField)
    Set p_SampleField = field
    p_sampleFields.add field
End Sub

'Expose the collection:
Public Property Get SampleFields() as Collection
    Set SampleFields = p_SampleFields
End Property

在常规模块中,您可以使用以下内容:

Sub Test()
    Dim sField as SampleField
    Dim sClass as SampleClass

    Set sField = New SampleField
    Set sClass = New SampleClass

    sField.Name = "SomeName"
    sClass.AddField sField 'This adds it to the collection

    'Access as per requirement:
    msgbox sClass.SampleFields(1).Name 'Pop-up saying "SomeName"
End Sub

【讨论】:

    【解决方案2】:

    对 Rik 的回答稍作改动,我们可以使用公共集合,省去 AddField 和 Get 方法:

    类 SampleClass:

    Public SampleFields As Collection
    
    Private Sub Class_Initialize()
        Set SampleFields = New Collection
    End Sub
    

    然后在你的模块中使用它:

    Sub Test()
        Dim sField as AnyOtherClass
        Dim sClass as SampleClass
    
        Set sField = New AnyOtherClass
        Set sClass = New SampleClass
    
        sField.Name = "SomeName"
        sClass.SampleFields.add sField 'This adds it to the collection
    
        'Access as per requirement:
        msgbox sClass.SampleFields(1).Name 'Pop-up saying "SomeName"
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-24
      • 2020-01-03
      • 2016-05-02
      相关资源
      最近更新 更多