VB选择案例不按预期工作

时间:2015-02-03 12:52:31

标签: vb.net select-case

我是一个全新的视觉基础和自我教学的人。我正在工作室2008中构建一个VB(我不得不使用这个版本),它登录到设备,传输登录和密码,然后使用反射传输从.txt文件读取的命令。所有这一切都很好。设备执行命令并输出28种可能的响应中的1种。

我正在使用精选案例来评估答案并采取相应行动。当在会话窗口中看到EXECUTED时,设备会话按预期停止,我的测试数据被设计为所以我得到的第一个响应是“EXECUTED”,奇怪的是我的VB“看到”EXECUTED消息(案例1)但是选择案例响应好像已经看到FAILED(案例2),测试数据的后续行违反了不同的情况(5和6),但响应始终是下一个案例。我试过Case n,case is = n,case“string value”但是我收到了错误。

这是我的代码 - 请注意,我还没有定义所有28个案例,但未定义的案例在我的活动版本中被REM了。我们将非常感激地提出任何想法或建议!

Option Explicit On
Public Class modCaseSelect

Shared Sub Dev_Responses(ByVal refl) 

    Dim Result As String
    Dim CR = vbCr
    Dim Resp As Integer

    Dim Dev_Resp(28) As String

    Dev_Resp(0) = "RUNNING"
    Dev_Resp(1) = "EXECUTED"
    Dev_Resp(2) = "FAILED"
    Dev_Resp(3) = "SEMANTICS ERROR"
    Dev_Resp(4) = "NONEXISTENT"
    Dev_Resp(5) = "NOT FOUND"
    Dev_Resp(6) = "SPECIAL"
    Dev_Resp(7) = "CONFIRM: Y/N"
    Dev_Resp(8) = "CONFIRM (Y/N)"
    Dev_Resp(9) = "CONFIRM EXECUTION: Y/N"
    Dev_Resp(10) = "ALREADY EXECUTED"
    Dev_Resp(11) = ""
    Dev_Resp(12) = ""
    Dev_Resp(13) = ""
    Dev_Resp(14) = ""
    Dev_Resp(15) = ""
    Dev_Resp(16) = ""
    Dev_Resp(17) = ""
    Dev_Resp(18) = ""
    Dev_Resp(19) = ""
    Dev_Resp(20) = ""
    Dev_Resp(21) = ""
    Dev_Resp(23) = ""
    Dev_Resp(23) = ""
    Dev_Resp(24) = ""
    Dev_Resp(25) = ""
    Dev_Resp(26) = ""
    Dev_Resp(27) = ""
    Dev_Resp(28) = "IN PROGRESS"

    With refl

        Select Case .WaitForStrings(Dev_Resp, "0:4:30") 'checkDev_Resp

            Case 0 ' "RUNNING"
                Result = Dev_Resp(0)
                Resp = MsgBox((Dev_Resp(0) & CR & CR & Continue?"), 17, "Case 0 error")

            Case 1 ' "EXECUTED"
                Result = Dev_Resp(1)
                Resp = MsgBox((Dev_Resp(1) & CR & CR & "Continue?"), 17, "Case 1")

            Case 2 ' "FAILED"
                Result = Dev_Resp(2)
                Resp = MsgBox((Dev_Resp(2) & CR & CR & "Continue?"), 17, "Case 2 error")

            Case 3 ' "SEMANTICS ERROR"
                Result = Dev_Resp(3)
                Resp = MsgBox((Dev_Resp(3) & CR & CR & "Continue?"), 17, "Case 3 error")

            Case 4 ' "NONEXISTENT"
                Result = Dev_Resp(4)
                Resp = MsgBox((Dev_Resp(4) & CR & CR & "Continue?"), 17, "Case 4 error")

            Case 5 ' "NOT FOUND"
                Result = Dev_Resp(5)
                Resp = MsgBox((Dev_Resp(5) & CR & CR & "Continue?"), 17, "Case 5 error")

            Case 6 ' "SPECIAL"
                Result = Dev_Resp(6)
                Resp = MsgBox((Dev_Resp(6) & CR & CR & "Continue?"), 17, "Case 6 error")

            Case 7 ' "CONFIRM: Y/N"
                Result = Dev_Resp(7)
                .Transmit("Y" & CR)

            Case 8 ' "CONFIRM (Y/N)"
                Result = Dev_Resp(8)
                .Transmit("Y" & CR)

            Case 9 ' "CONFIRM EXECUTION: Y/N"
                Result = Dev_Resp(9)
                .Transmit("Y" & CR)

            Case 10 ' "ALREADY EXECUTED"
                Result = Dev_Resp(10)
                Resp = MsgBox((Dev_Resp(10) & CR & CR & "Continue?"), 17, "Case 10 error")

            Case 11 ' ""
                Result = Dev_Resp(11)

            Case 12 ' ""
                Result = Dev_Resp(12)

            Case 13 ' ""
                Result = Dev_Resp(13)

            Case 14 ' ""
                Result = Dev_Resp(14)

            Case 15 ' ""
                Result = Dev_Resp(15)

            Case 16 ' ""
                Result = Dev_Resp(16)

            Case 17 ' ""
                Result = Dev_Resp(17)

            Case 18 ' ""
                Result = Dev_Resp(18)

            Case 19 ' ""
                Result = Dev_Resp(19)

            Case 20 ' ""
                Result = Dev_Resp(20)

            Case 21 ' ""
                Result = Dev_Resp(21)

            Case 22 ' ""
                Result = Dev_Resp(22)

            Case 23 ' ""
                Result = Dev_Resp(23)

            Case 24 ' ""
                Result = Dev_Resp(24)

            Case 25 ' ""
                Result = Dev_Resp(25)

            Case 26 ' ""
                Result = Dev_Resp(26)

            Case 27 ' ""
                Result = Dev_Resp(27)

            Case 28 ' "IN PROGRESS"
                Result = Dev_Resp(28)
                Resp = MsgBox((Dev_Resp(28) & CR & CR & "Continue?"), 17, "Case 28 error")

            Case Else

        End Select
    End With
End Sub
End Class

3 个答案:

答案 0 :(得分:0)

您在第一个"中缺少双引号Case。尝试将其更改为:

Case 0 ' "RUNNING"
    Result = Dev_Resp(0)
    Resp = MsgBox((Dev_Resp(0) & CR & CR & "Continue?"), 17, "Case 0 error")

注意我在"Continue?"之前添加了双引号。

答案 1 :(得分:0)

摆脱With声明。创建并分配持有者变量并将其与select语句一起使用。这样做将允许您通过在调试器中设置停止点来查看实际传递到select语句的内容。

Dim temp_resp as integer = refl.WaitForStrings(Dev_Resp, "0:4:30")
Select Case temp_resp
    'the case statements here.
End Select

答案 2 :(得分:0)

反射WaitForStrings使用从零开始的数组参数,但它返回一个基于1的字符串索引。 Waitforstrings将数组条目零视为第一个有效条目,因此第一个选择案例(Case = 1)对应于数组条目0。