gpt4 book ai didi

VBA 类 - 如何让一个类拥有额外的类

转载 作者:行者123 更新时间:2023-12-04 16:48:29 25 4
gpt4 key购买 nike

我正在尝试使用类来解决一个挑战。

我正在将事务记录到一个类中。

每笔交易都有以下内容:

  • 姓名
  • 日期
  • 时间
  • 说明

  • 但是,每笔交易也可以有许多具有以下属性的业务相关联系人
  • 业务联系人姓名
  • 业务区域
  • 账单百分比

  • 是否有任何示例说明如何做到这一点。

    我曾尝试为业务联系人添加第二个类,然后在事务类中构建一个集合,但都没有任何乐趣。

    我还尝试将业务联系方式作为交易类中的一个集合,但也没有任何乐趣。

    以下是我到目前为止所拥有的,但我可能已经走上了死胡同,可能不值得尝试拯救代码

    非常感谢任何帮助。

    谢谢
    J.P

    测试子 - 尝试将数据写入并取回
    Sub test()

    Dim x As Integer
    Dim xx As Integer

    'code to populate some objects
    Dim clocklist As Collection
    Dim clock As classClocks
    Dim businesscontactlist As Collection
    Dim businesscontact As classBusinessContact

    Set businesscontactlist = New Collection
    Set clocklist = New Collection

    For x = 1 To 3
    Set clock = New classClocks
    clock.LawyerName = "lawyer " & Str(x)
    For xx = 1 To 3
    businesscontact.Name = "Business Contact " & Str(xx)
    businesscontactlist.Add businesscontact

    Next xx
    clock.BusinessContactAdd businesscontactlist '----- errors here
    clocklist.Add clock
    Next x

    Set businesscontactlist = Nothing

    'write the data backout again
    For Each clock In clocklist
    Debug.Print clock.LawyerName
    Set businesscontactlist = clock.BusinessContacts
    For Each businesscontact In businesscontactlist
    Debug.Print businesscontact.Name
    Next

    Next

    End Sub

    时钟类 - 这是事务类
    Private pLawyerName As String
    Private pBusinessContactList As Collection

    Public Property Get LawyerName() As String
    LawyerName = pLawyerName
    End Property

    Public Property Let LawyerName(ByVal sLawyerName As String)
    pLawyerName = sLawyerName
    End Property

    Public Property Get BusinessContacts() As Collection
    Set BusinessContacts = pBusinessContactList
    End Property

    Public Property Set BusinessContactAdd(ByRef strName() As Collection)
    Set pBusinessContactList = New Collection
    Dim businesscontact As classBusinessContact
    Set businesscontact = New classBusinessContact

    For Each businesscontact In strName
    businesscontact.Name = strName.Item()
    pBusinessContactList.Add businesscontact
    Next
    End Property

    商务联系类 - 目前只有一处房产
    Private pBusinessContactName As String

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

    Public Property Let Name(ByVal sName As String)
    pBusinessContactName = sName
    End Property

    最佳答案

    有一些事情不会按照您在代码中的预期进行。我已经清理了一下,这个新版本应该更接近你想要的。如果这些更改不言自明,请告诉我。

    主要程序:

    Sub test()

    Dim i As Long
    Dim j As Long

    'code to populate some objects
    Dim clocklist As Collection
    Dim clock As classClocks
    Dim businessContactList As Collection
    Dim businessContact As classBusinessContact

    Set clocklist = New Collection

    For i = 1 To 3
    Set businessContactList = New Collection
    Set clock = New classClocks
    clock.LawyerName = "lawyer " & i
    For j = 1 To 3
    Set businessContact = New classBusinessContact
    businessContact.Name = "Business Contact " & j
    businessContactList.Add businessContact
    Next j
    Set clock.BusinessContactAdd = businessContactList
    clocklist.Add clock
    Next i

    Set businessContactList = Nothing

    'write the data backout again
    For Each clock In clocklist
    Debug.Print clock.LawyerName
    Set businessContactList = clock.BusinessContacts
    For Each businessContact In businessContactList
    Debug.Print businessContact.Name
    Next

    Next

    End Sub

    类时钟:
    Private pLawyerName As String
    Private pBusinessContactList As Collection

    Private Sub Class_Initialize()
    Set pBusinessContactList = New Collection
    End Sub

    Public Property Get LawyerName() As String
    LawyerName = pLawyerName
    End Property

    Public Property Let LawyerName(ByVal sLawyerName As String)
    pLawyerName = sLawyerName
    End Property

    Public Property Get BusinessContacts() As Collection
    Set BusinessContacts = pBusinessContactList
    End Property

    Public Property Set BusinessContactAdd(contactCollection As Collection)

    For Each contactName In contactCollection
    pBusinessContactList.Add contactName
    Next

    End Property

    关于VBA 类 - 如何让一个类拥有额外的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9686140/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com