dim m_col as New Collection
m_col.Add "001", "k001"
m_col.add "002", "k002"
m_col.add "003", "k003"
'执行到下一句出错:
debug.print m_col("k001") '而只能:
debug.pring m_col(1)为什么调用 m_col("k001") 会出错呢?
m_col.Add "001", "k001"
m_col.add "002", "k002"
m_col.add "003", "k003"
'执行到下一句出错:
debug.print m_col("k001") '而只能:
debug.pring m_col(1)为什么调用 m_col("k001") 会出错呢?
没错的.你安装个SP6的补丁试下吧.
类源代码如下:po
'事件
Public Event ProgressMin(ByVal vMin As Long) '设置最小值
Public Event ProgressMax(ByVal vMax As Long) '设置最大值
Public Event ProgressDescription(ByVal vDescription As String) '设置显示文本
Public Event Progress(ByVal vCurValue As Long) '当前进度值
Public Event ProgressFinished() '当前进度结束Public Event ErrCatch(ByVal vErrNo As Long, ByVal vErrDescription As String, ByVal vErrSource As String) '遇到错误
Public Event ExeResult(ByVal vResult As String) '处理结果,通过Result返回,只有有错误才触发
Private m_colChannelInfo As Collection
Private m_strConfigFile As StringPrivate m_cnnSupportCVSC As ADODB.ConnectionPublic Function GetCode(ByVal vTNSChannelCode As String) As String
GetCode = vTNSChannelCode
On Error Resume Next
'就是在下面这一句出的错!!!
GetCode = m_colChannelInfo(ChannelKeyCode(vTNSChannelCode))
End Function'--add end 050511Private Sub Class_Initialize()
End Sub
'配置文件(包含全路径)
Public Property Get PropConfigFile() As String
PropConfigFile = m_strConfigFile
End PropertyPublic Property Let PropConfigFile(ByVal vNewValue As String)
If Len(Trim(vNewValue)) <= 0 Then
RaiseEvent ErrCatch(vbObjectError, "配置文件不存在!", "cls")
Exit Property
End If
m_strConfigFile = vNewValue
m_strSupportCnnStr = GetIni(m_strConfigFile, "CTR", "SUPPORT_CNNSTR")
Set m_cnnSupportCVSC = New ADODB.Connection
m_cnnSupportCVSC.Open m_strSupportCnnStr
'初始化频道集合
InitColChannelCode
End PropertyPrivate Sub InitColChannelCode() Set m_colChannelInfo = New Collection
Dim tRs As ADODB.Recordset
Set tRs = m_cnnSupportCVSC.Execute("select ItemValue,PDBM from channel")
Do While Not tRs.EOF
m_colChannelInfo.Add tRs.Fields("PDBM") & "", ChannelKeyCode(tRs.Fields("ItemValue") & "")
tRs.MoveNext
Loop
tRs.Close
Set tRs = Nothing
End SubPrivate Function ChannelKeyCode(ByVal vMTRChannelCode As String) As String
ChannelKeyCode = CStr(vMTRChannelCode)
End Function
GetCode = m_colChannelInfo(ChannelKeyCode(vTNSChannelCode))=======================================================
你在立即窗口写下:
?ChannelKey(Code(vTNSChannelCode)) 回车看看输出结果是不是和你预计的(即集合中是否有此Key)一样,我想针对你的问题,应该是不一样的。
Collection 的 Key 最好使用显式的变量进行赋值,否则很容易出些莫名奇妙的问题
你可以修改 一下这段 Do While Not tRs.EOF
Dim Key as String
Dim Value as String
Value=tRs.Fields("PDBM") & ""
Key=tRs.Fields("ItemValue") & ""
m_colChannelInfo.Add Value,Key
tRs.MoveNext
Loop
tRs.Close
Set tRs = Nothing建议你改用 Dictionary ,因为无论速度还是稳定,都比 Collection 好的多,具体在 我的
Blog 都有说 http://blog.csdn.net/zlt982001/
按照你给的显示变量赋值试了,还是不行。
用Dictionary对象倒是没有什么问题了。