0

我正在为我的视觉基础课做作业。我已经编写了大部分代码,并且一切似乎都运行良好,除了我的 If Not 语句在循环找不到它要查找的内容时捕获异常。任何人都认为代码的外观有问题。该文件已使用浏览按钮加载,当我输入循环可以找到的信息时,它可以找到。

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As   
System.EventArgs)

Handles btnSearch.Click
    'event level variables
    Dim Found As Boolean
    Dim Counter As Integer

    'looks for entry match
    If rdoAbbrev.Checked = True Then
        Do Until Found Or Counter > 257
            If Country(Counter).Abbreviation.ToUpper = txtAbbrev.Text.ToUpper Then
                Found = True
                txtCountry.Text = Country(Counter).Names
            Else
                Counter += 1
            End If
        Loop
    Else
        Do Until Found Or Counter > 257
            If Country(Counter).Names.ToUpper = txtCountry.Text.ToUpper Then
                Found = True
                txtAbbrev.Text = Country(Counter).Abbreviation
            Else
                Counter += 1
            End If
        Loop
        If Not Found Then
            MessageBox.Show("This is not a valid entry.", "NO MATCH FOUND", MessageBoxButtons.OK)
            If rdoAbbrev.Checked = True Then
                txtAbbrev.Text = ""
                txtAbbrev.Focus()
            Else
                txtCountry.Text = ""
                txtCountry.Focus()

            End If
        End If
    End If
    'match not found response

    'reset variables
    Counter = 0
    Found = False
End Sub
4

3 回答 3

1

If Not Found阻塞仅在rdoAbbrev.Checked = True. 那是你的意图吗?如果不是,那么该代码块应该位于第一个If块之外(在其下方),或者您应该在第一个循环If之后有第二个块。While

编辑
看起来 Country 是一个数组。您可能应该使用Counter >= Country.Length.

VB.NET 中的数组是从 0 开始的。这意味着第一项位于Country(0),第二项位于Country(1),依此类推。如果数组中有 100 个元素,则最后一个元素位于Country(99)Country(100)不存在,如果您尝试访问它会导致异常。

我不确定您的作业要求是什么,但通常要遍历集合的元素(数组、列表等),您会使用 For 循环。您可以使用 Exit 命令提前退出循环。

For Counter As Integer = 0 To Country.Length - 1
     '...Country(Counter)
     If Found Then Exit For
Next
于 2012-10-05T18:14:24.653 回答
0

假设您希望无论属性如何都执行“未找到”部分rdoAbbrev.Checked,它看起来像是您的逻辑中的一个小错误(很容易修复)。

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
    'event level variables
    Dim Found As Boolean
    Dim Counter As Integer

    'looks for entry match
    If rdoAbbrev.Checked = True Then
        Do Until Found Or Counter > 257
            If Country(Counter).Abbreviation.ToUpper = txtAbbrev.Text.ToUpper Then
                Found = True
                txtCountry.Text = Country(Counter).Names
            Else
                Counter += 1
            End If
            'You could also write this as:
            'Found = Country(Counter).Abbreviation.ToUpper = txtAbbrev.Text.ToUpper
            'If Found Then
            '    txtCountry.Text = Country(Counter).Names
            'Else
            '    Counter += 1
            'End If
        Loop
    Else
        Do Until Found Or Counter > 257
            If Country(Counter).Names.ToUpper = txtCountry.Text.ToUpper Then
                Found = True
                txtAbbrev.Text = Country(Counter).Abbreviation
            Else
                Counter += 1
            End If
            'You could also write this as:
            'Found = Country(Counter).Names.ToUpper = txtCountry.Text.ToUpper
            'If Found Then
            '    txtAbbrev.Text = Country(Counter).Abbreviation
            'Else
            '    Counter += 1
            'End If
        Loop
    End If
    'match not found response
    'Move your "Not Found" here so that the not found works regardless of the rdoAbbrev.Checked property.
    If Not Found Then
        MessageBox.Show("This is not a valid entry.", "NO MATCH FOUND", MessageBoxButtons.OK)
        If rdoAbbrev.Checked = True Then
            txtAbbrev.Text = ""
            txtAbbrev.Focus()
        Else
            txtCountry.Text = ""
            txtCountry.Focus()

        End If
    End If

    'reset variables
    Counter = 0
    Found = False
End Sub
于 2012-10-05T18:14:18.293 回答
-1

可能您应该在代码的范围内结束 If 语句,并避免在代码行末尾结束所有 If 语句。通常在 Basic 中对我有用。我认为 Basic 有很多优点,但对我来说,它仍然不是一种有问题的高级语言,因为它很容易使用。

于 2014-01-11T19:34:21.320 回答