选择Case&尝试语句无限循环

时间:2016-08-10 16:22:43

标签: vb.net try-catch infinite-loop select-case

当我尝试在我的vb.net项目中打印报告时,我有一些代码来检查是否已填写所有必填字段。如果不是,则会出现一个消息框以通知用户。当他们按“确定”时,我需要程序停止执行代码来加载报告。目前,它被卡在一个无限循环中,在那里它反复通过Try函数和SELECT CASE

需要改变什么来阻止这种情况?我无法弄清楚问题是什么,以及为什么以下代码部分一个接一个地循环

Try
    Select Case checkwhat.ToUpper
        Case "SUPPLIER"
            If cmbSuppliers.Text.Trim = "" Then
                MsgBox("Please select a supplier", MsgBoxStyle.OkOnly, "No Supplier Selected")
                Return False
                Exit Try
            End If
        Case "RB6B"
            check("SUPPLIER")
            If check("SUPPLIER") = True Then  Else Exit Sub
            createWorkTable("SUPPLIERS-TERRITORY-LS")
            regReport("rTerritoryWTableCrosstabB.rpt", "", con, Me.MdiParent, cReport, True)
            fillPms(cReport, "Sales by Territory by Supplier", "For " & cmbSuppliers.Text.Trim, "", "AOT02")

1 个答案:

答案 0 :(得分:2)

这不是一个完整的答案,但我至少可以解决一些问题:

Select Case checkwhat.ToUpper
    Case "SUPPLIER"
        If cmbSuppliers.Text.Trim = "" Then
            MsgBox("Please select a supplier", MsgBoxStyle.OkOnly, "No Supplier Selected")
            Return False
            Exit Try '<- Redundant
        Else
            ' Perform other checks
            Return True
        End If

因此,这意味着假设您输入供应商案例,您至少可以获得两个返回值。

现在..在您的RB6B案例中,您运行check功能两次。

Case "RB6B"    
    check("SUPPLIER")
    If check("SUPPLIER") = True Then

你有两个选择:

Dim supplierValid as Boolean = check("SUPPLIER")
If supplierValid = True

或者只删除第一行

If check("SUPPLIER") = True Then

好。所以在那之后:

If check("SUPPLIER") = True Then Else Exit Sub

尝试并避免使用此样式,因为它可能会隐藏代码路径,并且您可能会在未实现的情况下遗漏某些功能。两个选项如下所示。一个避免额外的嵌套

If check("SUPPLIER") = True Then  
    createWorkTable("SUPPLIERS-TERRITORY-LS")
    regReport("rTerritoryWTableCrosstabB.rpt", "", con, Me.MdiParent, cReport, True)
    fillPms(cReport, "Sales by Territory by Supplier", "For " & cmbSuppliers.Text.Trim, "", "AOT02")
Else 
    Exit Sub
End If

或者

If check("SUPPLIER") = False Then
    Exit Sub
End If  

createWorkTable("SUPPLIERS-TERRITORY-LS")
regReport("rTerritoryWTableCrosstabB.rpt", "", con, Me.MdiParent, cReport, True)
fillPms(cReport, "Sales by Territory by Supplier", "For " & cmbSuppliers.Text.Trim, "", "AOT02")