将项目添加到 VBA/VB6 集合时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3369596/
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
Errors adding items to a VBA/VB6 Collection
提问by holografix
I'm still learning VBA and I can't figure out wth I'm having so many problems with a Collections object.
我仍在学习 VBA,我无法弄清楚我在 Collections 对象上有这么多问题。
I have a function that adds custom objects (I created a very simple class to store some data) that does the typical "read data, create object representation, stick it into Collections" sort of stuff.
我有一个添加自定义对象的函数(我创建了一个非常简单的类来存储一些数据),它执行典型的“读取数据,创建对象表示,将其粘贴到集合中”之类的东西。
If I try to add a "key" to the bag.add call I get a "Compile error. Expected:=" message.
如果我尝试向 bag.add 调用添加“密钥”,则会收到“编译错误。预期:=”消息。
If I don't it appears to have worked then when I run the program it says "Compile Error. Argument not optional" and highlights the "getRevColumns = bag" line.
如果我不这样做,那么当我运行该程序时,它会显示“编译错误。参数不可选”并突出显示“getRevColumns = bag”行。
I can't for the life of me figure out wth is going on! I suspect something wrong with how I initialized my bag?! PS: columnMap is the name of my custom class.
我一生都无法弄清楚发生了什么!我怀疑我初始化包的方式有问题?!PS:columnMap 是我自定义类的名称。
Function getRevColumns() As Collection
Dim rng As Range
Dim i As Integer
Dim bag As Collection
Dim opManCol As Integer, siebelCol As Integer
Dim opManColName As String, siebelColName As String
Dim itm As columnMap
Set bag = New Collection
Set rng = shSiebelMap.UsedRange.Columns(5)
i = 1
For i = 1 To rng.Rows.count
If StrComp(UCase(rng.Cells(i).value), "Y") = 0 Then
opManCol = rng.Rows(i).OffSet(0, -2).value
opManColName = rng.Rows(i).OffSet(0, -4)
siebelCol = rng.Rows(i).OffSet(0, -1).value
siebelColName = rng.Rows(i).OffSet(0, -3)
Set itm = New columnMap
itm.opManColName = opManColName
itm.opManColNumber = opManCol
itm.siebelColName = siebelColName
itm.siebelColNumber = siebelCol
'WHY DOESN'T IT WORK!''
bag.Add (itm)
'MsgBox "opMan Col: " & opManColName & " : " & opManCol & ". Siebel Col: " & siebelColName & " : " & siebelCol'
End If
Next i
getRevColumns = bag
End Function
回答by Vincent
Try removing the parens around itm in the add:
尝试在添加中删除 itm 周围的括号:
bag.Add itm
or
或者
bag.Add itm, key
It's been a while since I've had to work with VBA/VB6, but I believe including the parens causes itm to be passed by value instead of by reference. I could be wrong.
自从我不得不使用 VBA/VB6 以来已经有一段时间了,但我相信包括括号会导致它通过值而不是通过引用传递。我可能是错的。
回答by renick
the bag is an object. Rule #1 for objects use Set
包是一个对象。对象的规则 #1 使用 Set
Set getRevColumns = bag
回答by hol
You need to say
你需要说
set getRevColumns = bag
also I guess you have a problem on the add. I don't know why this is but it works on
另外我猜你在添加上有问题。我不知道为什么会这样,但它有效
bag.add itm
I tried the whole thing in a simple manner here is my working code
我以简单的方式尝试了整个过程,这是我的工作代码
Sub myroutine()
Dim bag As Collection
Dim itm As clsSimple
Set bag = getTheCollection()
Set itm = bag.Item(1)
MsgBox (itm.someObjectValue)
Set itm = bag.Item(2)
MsgBox (itm.someObjectValue)
End Sub
Function getTheCollection() As Collection
Dim bag As Collection
Dim itm As clsSimple
Set bag = New Collection
Set itm = New clsSimple
itm.someObjectValue = "value 1"
bag.Add itm
Set itm = New clsSimple
itm.someObjectValue = "value 2"
bag.Add itm
Set getTheCollection = bag
End Function
The class is really simple:
这个类非常简单:
Public someObjectValue As String
Hope it helps
希望能帮助到你
回答by Robin Sandlin - Developer
I had a similar problem with a collection.
我在收藏方面遇到了类似的问题。
I Dim'd it but hadn't set it with New or initialized it.
我把它调暗了,但没有用 New 设置它或初始化它。
Basically i had
基本上我有
Dim collection1 As Collection
...
collection1.Add item 'no compile error just empty
I added the following before the add
我在添加之前添加了以下内容
Set collection1 = New Collection
Call collection1.init
then it worked like a charm...I had also moved the Dim statement from the Sub to the top of the Module to make it a class variable
然后它就像一个魅力......我还将 Dim 语句从 Sub 移动到模块的顶部,使其成为类变量