2009-11-11 20 views
1

我一直在努力,为什么下面的代码,通过循环第三次我得到一个错误类型时行“对于lCount = 0至MAXCOUNT”正在13不匹配,找出评估。我原本以为这个问题是从vArray获得价值,但测试显示它是由“For”行触发的。我不知道在循环处理过程中类型将如何改变。谢谢!VB6类型不匹配在循环条件

Public Function FindCodeIndex(vArray As Variant, MatchValue As String) As Integer 
    ''This function locates a value in a combo box returning the index or -1 if not found 
    Dim lCount As Long 
    Dim maxCount As Long 
    Dim arrayStr As String 


    On Error GoTo ErrorHandler 

    maxCount = UBound(vArray) 


    For lCount = 0 To maxCount 
    arrayStr = vArray(1, lCount) 

     If UCase$(arrayStr) = UCase$(MatchValue) Then 
      FindCodeIndex = Int(lCount) 
      Exit Function 
     End If 
    Next lCount 

    FindCodeIndex = -1 

    Exit Function 


ErrorHandler: 

MsgBox "Unexpected error in frmComment::FindCodeIndex()" & vbCrLf & _ 
      "Error Code: " & CStr(Err.Number) & " Error Desc: " & Err.Description 
+1

您不会将'arrayStr'赋值给任何值。你确定这不是问题吗? – Dan 2009-11-11 23:09:55

+0

添加了任务(从内存,不在工作PC现在)到arrayStr。测试显示循环在第三遍的For语句失败。 – Timbuck 2009-11-12 01:22:16

+0

蒂姆,你可以用当地人的窗口看到,在第三道次所用的阵列中的数据,并编辑您的问题,包括这个信息。 – 2009-11-14 06:32:28

回答

1
Public Function FindCodeIndex(Array() As String, ByVal MatchValue As String) As Long 

    Dim index As Long 
    Dim upper_bound As Long 

    upper_bound= UBound(Array) 
    MatchValue = UCase(MatchValue) 

    For index = 0 To upper_bound 
     If UCase(Array(index)) = MatchValue Then 
      FindCodeIndex = index 
      Exit Function 
     End If 
    Next index 

    FindCodeIndex = -1 

End Function 
+0

VB绝对不喜欢那个解决方案,抱怨Array()没有被定义。 最近的测试表明,设置一个长= -1也抛出一个错误(?) – Timbuck 2009-11-13 00:50:00

+0

@Timbuck。 'Array'不是VB6中的有效标识符,因为它与内置的'Array'函数冲突。 – 2009-11-19 03:39:56

+0

没错,这就是为什么我不能确定为什么ChaosPandion这里把这个代码,除非我失去了一些东西。 – Timbuck 2009-11-21 03:12:20

0

的功能提到的代码被用于一个组合框写入(你实际复制每个项目在list()方法到一个数组,发送给你的功能?)。如果你使用标准的VB组合框,这看起来有点过于复杂。只需使用以下代码:

Private Declare Function SendMessage Lib "User32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal uMsg As Long, ByRef wParam As Any, ByRef lParam As Any) As Long 

Private Const CB_FINDSTRINGEXACT As Long = &H158 

Public Function FindCodeIndex(ByRef cmb As ComboBox, ByRef sMatchValue As String) As Long 
'This function locates a value in a combo box returning the index or -1 if not found 

    FindCodeIndex = SendMessage(cmb.hWnd, CB_FINDSTRINGEXACT, ByVal -1, ByVal sMatchValue 

End Function 

在这种情况下使用Windows API的速度会更快,更小。

+0

这稍差评论我的一部分 - 也涉及到其他开发人员制作大约十年前一个错误的决定。实际上,我所拥有的是一个有两行的数组,第一个是代码描述列表,第二个是关联的代码列表。上面的函数看起来通过阵列的匹配代码值,并且将组合框LISTINDEX =到阵列的项目被发现的位置。组合框显示代码说明。 – Timbuck 2009-11-21 03:10:48