Excel 如何获取对象';s在集合中的位置? 先决条件:
我有:Excel 如何获取对象';s在集合中的位置? 先决条件:,excel,vba,class,collections,Excel,Vba,Class,Collections,我有: 一个名为“users”的集合 它由名为“MatchClass”的类的实例组成 “MatchClass”仅由两个属性组成—userid和matchArray(加上一些与问题无关的方法) 我想做什么 是在集合上循环,同时能够 访问集合中的位置 访问类实例的属性 从所有matchArrays(它们已经排序)中找到最高值,并存储userid 到目前为止,我所尝试的: 在集合上循环,每个循环使用 不幸的是,这也不行,因为我无法访问属性 通过users.Item(i)code访问Matc
- 一个名为“
”的users
集合
- 它由名为“
”的MatchClass
类的实例组成
- “
”仅由两个属性组成—MatchClass
和userid
(加上一些与问题无关的方法)matchArray
我想做什么 是在
集合上循环,同时能够
- 访问集合中的位置
- 访问类实例的属性
- 从所有
matchArray
s(它们已经排序)中找到最高值,并存储userid
到目前为止,我所尝试的:
在集合上循环,每个循环使用
不幸的是,这也不行,因为我无法访问属性
通过users.Item(i)
code访问MatchClass
实例
是否有一个两全其美的解决方案,我可以同时做到这两个方面?
我觉得这应该是我所缺少的一些武断的东西。根据我的评论,我希望这两种方法中的任何一种都能起作用。很高兴被证明是错误的,因为他们没有经过测试
每人
为了
如果我理解了你的问题,那么我会认为上述两种方法中的任何一种(如果不是两种)都能得到你想要的结果。根据我的评论,我希望这两种方法中的任何一种都能奏效。很高兴被证明是错误的,因为他们没有经过测试
每人
为了
如果我理解了你的问题,那么我会认为上述任何一个(如果不是两个)都能得到你想要的结果。
- 如果
MatchClass
是一个对象,而users
是MatchClass
实例的集合,那么您应该使用Set
关键字来分配对象引用(我指的是此行maxuser=users.Item'max
最大值=温度
设置maxuser=user'
- 如果
MatchClass
是一个对象,而users
是MatchClass
实例的集合,那么您应该使用Set
关键字来分配对象引用(我指的是此行maxuser=users.Item'max
最大值=温度
设置maxuser=user'是否可能需要集合的索引,就像在For-Each循环中包含一个索引,并在每次循环时将其递增1一样。我可能错了,但我确信您可以在For循环中执行所需的操作。只需强制转换项(users.item(I))对于MatchClass变量。是否可能希望集合的索引像在For-Each循环中包含一个索引并在每次循环时将其递增1那样简单。我可能错了,但我相信您可以在For循环中执行所需的操作。只需强制转换项(users.item(I))到MatchClass变量。从技术上讲,这并不能回答我的问题,因为我在寻找“用户”的索引集合中的MatchClass,但我对nontheless进行了投票,因为将maxuser
称为MatchClass
的一个实例,而不是String
-我基本上将您的答案与@skin结合在一起,这并没有回答我的问题,因为我在寻找“用户”的索引集合中的MatchClass,但我对nontheless投了更高的票,因为将maxuser
称为MatchClass
的一个实例,而不是String
-我基本上将你的答案与@SkinOh yeah lol结合起来,这完全超出了我的理解。有时我们试图找到最优雅、最有趣的答案错综复杂的解决方案,同时我们忘记了最基本的解决方案。@Rawrplus是的,我们忘记了。妻子经常告诉我我想得太多了。哦,是的,哈哈,这完全超出了我的头脑。有时我们试图找到最优雅和错综复杂的解决方案,同时我们忘记了最基本的解决方案。@Rawrplus是的,我们忘记了。妻子经常这样做告诉我我想得太多了。
Dim users as New Collection
Dim i as Byte
Dim max as Integer: max = 0
Dim maxuser as String
' users Collection is filled with instances of MatchClass
' ... skipping code to simplify ...
Dim user as MatchClass
For Each user in users
temp = user.matchArray(0, 0)
If temp > max Then
max = temp
maxuser = users.Item ' <- this won't work
End If
Next user
For i = users.Count to 1 Step -1
users.Item(i).matchArray(0, 0) ' <- this won't work
Next i
Dim users As New Collection
Dim i As Byte
Dim max As Integer: max = 0
Dim maxuser As String
' users Collection is filled with instances of MatchClass
' ... skipping code to simplify ...
Dim user As MatchClass
Dim lngIndex As Long
For Each user In users
lngIndex = lngIndex + 1
temp = user.matchArray(0, 0)
If temp > max Then
max = temp
maxuser = lngIndex
End If
Next user
Dim user As MatchClass
For i = users.Count To 1 Step -1
Set user = users.item(i)
matchArray = item.matchArray(0, 0)
Next i
Private Sub FindMaxUser()
Dim users As New Collection
Dim i As Byte
Dim max As Long: max = 0
Dim temp As Long
' ... skipping code to simplify ...
Dim user As MatchClass
Dim maxuser As MatchClass ' <- Shouldn't be a string.
For Each user In users
temp = user.matchArray(0, 0)
If temp > max Then
max = temp
Set maxuser = user ' <- MatchClass is an object. "Set" keyword is required for object references
End If
Next user
End Sub