类对象数组作为 VBA 中的类成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28309235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Array of class objects as class member in VBA
提问by abalter
I'm writing an Excel macro in VBA to send emails to library patrons alerting them of overdue materials. The data comes from a spreadsheet with data like
我正在用 VBA 编写一个 Excel 宏,以向图书馆顾客发送电子邮件,提醒他们过期材料。数据来自包含数据的电子表格,例如
UserID Name Email Title Author Barcode Call Number Borrowed Date Due Date
user2 Jones, Bob bob@jones Title1 A. Baker a0001 H00027C 11/23/2014 1/1/2015
user2 Jones, Bob bob@jones Title2 C. Dalton b0002 S00406R 11/23/2014 1/1/2015
user3 Smith, Mary bob@jones Title3 E. Franklin c0003 M00174R 11/23/2014 1/1/2015
user3 Smith, Mary mary@smith Title4 F. Gelt d0004 M00157G 11/23/2014 1/1/2015
user3 Smith, Mary mary@smith Title5 H. Ivers e0005 M00081G 11/23/2014 1/1/2015
I started out with a user defined type for book and patron, and gave each patron an array of books. I tried passing the patron to a function to generate the email text, but apparently I can't pass a user defined type as a parameter. So, now I'm trying classes.
我从用户定义的书籍和赞助人类型开始,并为每个顾客提供了一系列书籍。我尝试将顾客传递给一个函数来生成电子邮件文本,但显然我无法将用户定义的类型作为参数传递。所以,现在我正在尝试课程。
I want to have a Bookclass with members
我想Book和成员一起上课
Public book_title As String
Public date_borrowed As Date
Public date_due As Date
Public barcode As String
Public call_number As String
and a EmailDataclass with members
和一个EmailData有成员的班级
Public email_text As String
Public patron_name As String
Public patron_email As String
Public sender_email As String
Public book_list() As Book
where book_listis an array of Bookobjects, and a method sendEmail().
wherebook_list是一个Book对象数组和一个方法sendEmail()。
But, this isn't working because apparently I can't define an array of Bookobjects as a member of the an EmailDataobject.
但是,这不起作用,因为显然我无法将Book对象数组定义为对象的成员EmailData。
So, what is my approach here? I've heard of collections. Would that be a solution?
那么,我在这里的方法是什么?我听说过收藏。这会是一个解决方案吗?
回答by Sorceri
Per my comment I would use a collection. Here is how you define it and initialize it
根据我的评论,我会使用一个集合。这是定义和初始化它的方法
Public book_list As Collection
'intitalize the collection in the constructor of the class
Private Sub Class_Initialize()
Set book_list = New Collection
End Sub
and to use it
并使用它
book_list.Add <book>
dim bk as Book
set bk = book_list.Item(indexNumber)
回答by Tmdean
There's a couple of options.
有几个选择。
The easiest is just to make book_list a Variant type. You can treat it as an array using ReDim. You can use variables of Variant type to store object references just like you would with an object variable.
最简单的方法就是让 book_list 成为 Variant 类型。您可以使用 ReDim 将其视为数组。您可以使用 Variant 类型的变量来存储对象引用,就像使用对象变量一样。
The other option is to make book_list a private variable and create accessor methods. This is the preferred way to use classes anyway, and if you're using classes in the first place, you might as well observe good object oriented design. Your class would look something like this.
另一种选择是使 book_list 成为私有变量并创建访问器方法。无论如何,这是使用类的首选方式,如果您首先使用类,您不妨观察良好的面向对象设计。你的班级看起来像这样。
Private email_text As String
...
Private book_list() As Book
Private Sub Class_Initialize()
email_text = "default email text"
...
ReDim book_list(10)
End Sub
Public Function GetBook(index As Long) As Book
Set GetBook = book_list(index)
End Function
Public Sub SetBook(index As Long, b As Book)
Set book_list(index) = b
End Sub

