|
本帖最后由 suihu 于 2020-1-24 11:13 编辑
通过类模块定义了一个类“PersonClass”
通过 Collection类型 将多个 PersonClass 放到了 集合中
但再从 Collection 类型中获取 PersonClass实例时如何实现强制类型转换?
代码片断:
1、PersonClass类对象
- Option Explicit
- Public code As Integer '序号
- Public name As String '姓名
- Public cardId As String '身份证号
- Public huCode As String '户号
- Public isHuzhu As String '是否户主
复制代码
2、personClass实例赋值
- Dim I As Integer
- Dim rowIndex As Integer
- Dim colIndex As Integer
- Dim rowCount As Integer
- Dim sequence As Integer
- Dim getPerson As PersonClass
-
- Dim hujiDatas As New Collection
- For I = rowIndex To 3
- Dim person As New PersonClass
- person.code = sequence
- person.name = Range("B" + CStr(rowIndex)).Value
- person.cardId = Range("C" + CStr(rowIndex)).Value
- person.huCode = Range("D" + CStr(rowIndex)).Value '
- person.isHuzhu = Range("E" + CStr(rowIndex)).Value
-
- sequence = sequence + 1
- rowIndex = rowIndex + 1
-
- hujiDatas.Add Item:=person, Key:=person.cardId
-
- Next
复制代码
3、获取集合中personClass实例
- For I = 1 To hujiDatas.Count
-
- Set getPerson = New PersonClass
- getPerson = hujiDatas.Item(I)
- MsgBox p.name
-
- Next
复制代码
第3部分:getPerson = hujiDatas.Item(I) 出错
出错提示:对象不支持该属性或方法
求助
|
|