加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

VB自定义类来替换collection设置Key和Value值

发布时间:2020-12-16 22:43:15 所属栏目:大数据 来源:网络整理
导读:创建一个类模块,命名为MyCollection 代码如下: Option Explicit Private colKey As New Collection Private colVal As New Collection Public Property Get Count() As Long Count=colKey.Count End Property Public Property Get RealCol() As Collection

创建一个类模块,命名为MyCollection
代码如下:

 
 
  1. OptionExplicit
  2. PrivatecolKeyAsNewCollection
  3. PrivatecolValAsNewCollection
  4. PublicPropertyGetCount()AsLong
  5. Count=colKey.Count
  6. EndProperty
  7. PublicPropertyGetRealCol()AsCollection
  8. SetRealCol=colVal
  9. EndProperty
  10. PublicSubAdd(Item,OptionalKey,OptionalBefore,OptionalAfter)
  11. colKey.AddKey,Key,Before,After
  12. colVal.AddItem,After
  13. EndSub
  14. PublicSubRemove(index)
  15. colKey.Removeindex
  16. colVal.Removeindex
  17. EndSub
  18. PublicFunctiongetKey(index)AsString
  19. getKey=colKey.Item(index)
  20. EndFunction



下面我们用一些代码来测试一下collection特性是否保留,并且我们能不能通过索引号获得key
建个窗体,放个按钮

 
 
  1. PrivateSubCommand1_Click()
  2. DimaAsNewMyCollection'创建实例
  3. '加入对象
  4. a.Add"test1","key1"
  5. a.Add"test2","key2"
  6. a.Add"test3","key3"
  7. a.Add"test4","key4"
  8. a.Add"test5","key5"
  9. Debug.Print"用索引号方式获取对象"
  10. Debug.Printa.RealCol(2)
  11. Debug.Print"用key获取对象"
  12. Debug.Printa.RealCol("key3")
  13. Debug.Print"remove对象"
  14. a.Remove"key2"
  15. Debug.Print"foreach访问"
  16. Dimb
  17. ForEachbIna.RealCol
  18. Debug.Printb
  19. Next
  20. Debug.Print"罗列所有key"
  21. DimiAsInteger
  22. Fori=1Toa.Count
  23. Debug.Printa.getKey(i)
  24. Next
  25. Debug.Print"异常测试"
  26. a.Add"asdad","key5"'重复key
  27. a.Remove"key6"'删除不存在的对象
  28. a.getKey8'获取不存在的key
  29. EndSub

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读