gpt4 book ai didi

Excel VBA在类中声明一个arraylist

转载 作者:行者123 更新时间:2023-12-04 10:03:32 25 4
gpt4 key购买 nike

我正在尝试创建一个 Staff 类模块,其中包含姓氏等字符串,以及一个用于存储/调用 0-10 个字符串之间的数组列表,具体取决于使用时添加了多少个字符串。

类模块称为 StaffClass 并包含:

Private m_surname As String
Private m_districts as ArrayList

' Surname Prop

Property Get surname() As String
surname = m_surname
End Property

Property Let surname(surname As String)
m_surname = Name
End Property

' District Prop
' This is where i'm getting confused

Private Sub Class_ArrList()
Set m_districts = New ArrayList
End Sub

Property Get districts() As ArrayList
districts = m_districts
End Property

Property Let districts(districts as ArrayList)
m_districts = districts
End Property

主模块包含:
Dim newStaff As StaffClass

Set newStaff = New StaffClass

newStaff.surname = "Smith"

' This is where I want to add to the arraylist

newStaff.districts(0) = "50"

我知道我缺少负载,但很难找到与 VBA 类中的集合相关的很多内容。

希望你能帮忙!

最佳答案

您可以将 arraylist 初始化例程放在 Class_Initialize 中。 , 并向类添加一个方法来添加/插入/等每个项目。 (或者您可以添加一个方法将数组列表添加为单个对象)。

此外,由于 ArrayList是一个对象,你需要使用 Set检索时的关键字。

例如:

类(class)模块

Option Explicit

Private m_surname As String
Private m_districts As ArrayList

' Surname Prop

Property Get surname() As String
surname = m_surname
End Property

Property Let surname(surname As String)
m_surname = surname
End Property

Property Get districts() As ArrayList
Set districts = m_districts
End Property

Function addDistrict(Value As String)
m_districts.Add Value
End Function

Private Sub Class_Initialize()
Set m_districts = New ArrayList
End Sub


常规模块
Option Explicit
Sub par()
Dim newStaff As StaffClass
Dim V As ArrayList

Set newStaff = New StaffClass

With newStaff
.surname = "Smith"
.addDistrict 50
.addDistrict "xyz"
End With

Set V = newStaff.districts

Stop
End Sub

关于Excel VBA在类中声明一个arraylist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61706647/

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