您的位置:程序门 -> vb -> 基础类



vb collection 如何提取key 值出来啊?在线等


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


vb collection 如何提取key 值出来啊?在线等[已结贴,结贴人:kelvin357]
发表于:2007-09-20 16:08:15 楼主
为什么vb   的   collection   只能取出   value   值   ,   不能取出key   值啊,   这样很不方便   ,   vb   有什么集合可以这样做啊?
发表于:2007-09-20 16:30:451楼 得分:0
不可以
你想实现什么目的呢?
发表于:2007-09-20 16:36:012楼 得分:0
recordset可以实现你说的应该

完全自己拼recordset出来
发表于:2007-09-20 16:36:323楼 得分:0
或者就是用数据库了
在数据库里面建个表
然后读表
发表于:2007-09-20 16:49:174楼 得分:0
我有一些数据,   用collection   保存了key   ,value     .   而那个key   对我来说,   很重要,   是用来与另外一些数据作对比的,   例如是listview   里的某一个列column,   如果我的   collection   的key=   listview   的column   ,   那个,   listview   就会被插入一条新的列,而那条列的值就是collection   的value   ,   我的想法就是这样,collection   是临时保存在内存的,那个索引的key   是跟listview   的某个列保存出来的.这样做了对应,   才能把collection   的value   正确插入到listview   的位置   上
发表于:2007-09-20 17:07:205楼 得分:0
也就是说你要判断collection中某一个key是否存在?
发表于:2007-09-20 17:18:446楼 得分:0
我想取出collection   中的key   值与listview   中的某一列的值来进行对比,   如果key   =   listview   .listitem(1).subitems(5),这样,   我就把collection   的value   取出,   插入到   listview   另一列中
发表于:2007-09-20 17:20:307楼 得分:0
我觉得可以通过捕捉错误来处理吧?
比如:

代码中用:on   error   resume   next,然后:
if   iserror(col.item( "key_name "))   then   ......
如果这个key_name的key不存在,iserror返回trun
发表于:2007-09-20 17:25:218楼 得分:5
试了下,可以自己写个函数:
function   contains(byval   c   as   collection,   byval   skey   as   string)   as   boolean        
on   error   goto   err_c
        dim   v
        v   =   c.item(skey)
        contains   =   true
        exit   function
err_c:
        if   err.number   =   5   then   contains   =   false
        '不存在返回false
end   function


再这样处理:
if   not   contains(col,   cstr(listview   .listitem(1).subitems(5)))   then
  listview添加数据
end   if

试试吧,可能不是最好的方法,但应该可以实现吧?
看看楼下还有什么好方法


发表于:2007-09-20 17:28:249楼 得分:0
添加删除集合对象的时候在另外一个数组或者集合里面记录key,以备查询
发表于:2007-09-20 18:01:4710楼 得分:0
chewinggum(口香糖·第二次减肥计划执行中)   (   )  
添加删除集合对象的时候在另外一个数组或者集合里面记录key,以备查询
-----------------------------------------------------------------------
不是很懂你的想法,   可不可以说易明白一点??
发表于:2007-09-20 20:38:3511楼 得分:5
可以自己写一个类来替换collection
下面这段代码只是随性写的,可能还存在部分考虑不完善的地方,看看有没有人进一步补充完善

创建一个类模块,命名为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
        set   realcol   =   colval
end   property
public   sub   add(item,   optional   key,   optional   before,   optional   after)
        colkey.add   key,   key,   before,   after
        colval.add   item,   key,   before,   after
end   sub
public   sub   remove(index)
        colkey.remove   index
        colval.remove   index
end   sub

public   function   getkey(index)   as   string
        getkey   =   colkey.item(index)
end   function


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

private   sub   command1_click()
dim   a   as   new   mycollection   '创建实例

'加入对象
a.add   "test1 ",   "key1 "
a.add   "test2 ",   "key2 "
a.add   "test3 ",   "key3 "
a.add   "test4 ",   "key4 "
a.add   "test5 ",   "key5 "
debug.print   "用索引号方式获取对象 "
debug.print   a.realcol(2)
debug.print   "用key获取对象 "
debug.print   a.realcol( "key3 ")
debug.print   "remove对象 "
a.remove   "key2 "

debug.print   "for   each访问 "
dim   b
for   each   b   in   a.realcol
        debug.print   b
next
debug.print   "罗列所有key "
dim   i   as   integer
for   i   =   1   to   a.count
        debug.print   a.getkey(i)
next

debug.print   "异常测试 "
a.add   "asdad ",   "key5 "   '重复key
a.remove   "key6 "           '删除不存在的对象
a.getkey   8   '获取不存在的key


end   sub
发表于:2007-09-20 22:48:5412楼 得分:0
可以改用scripting的dictionary对象.
发表于:2007-09-21 10:29:1213楼 得分:0
谢谢vbman2003(家人)   ,chewinggum(口香糖·第二次减肥计划执行中)   热心帮助
发表于:2007-09-21 12:18:0014楼 得分:0
我通过了chewinggum(口香糖·第二次减肥计划执行中)   的方法,   解决了问题,   谢谢
发表于:2007-09-22 16:47:1315楼 得分:0
结贴啦?


快速检索

最新资讯
热门点击