Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net VisualBasic如果没有问题_Vb.net_Exception_Loops - Fatal编程技术网

Vb.net VisualBasic如果没有问题

Vb.net VisualBasic如果没有问题,vb.net,exception,loops,Vb.net,Exception,Loops,我正在为我的visual basic课程做作业。我已经编写了大部分代码,除了我的If Not语句之外,其他一切似乎都运行得很好,该语句在循环找不到它要查找的内容时捕获异常。任何人都会发现代码的外观有问题。文件已经使用browse按钮加载,当我输入循环可以找到的信息时,它就可以工作了 Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearc

我正在为我的visual basic课程做作业。我已经编写了大部分代码,除了我的If Not语句之外,其他一切似乎都运行得很好,该语句在循环找不到它要查找的内容时捕获异常。任何人都会发现代码的外观有问题。文件已经使用browse按钮加载,当我输入循环可以找到的信息时,它就可以工作了

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
假设您希望执行“未找到”部分,而不考虑
rdoAbbrev.Checked
属性,它看起来像是逻辑中的一个轻微错误(很容易修复)


只有在
rdoAbbrev.Checked=True
时,才会出现
If Not Found
块。这是你的本意吗?如果不是,则该代码块应该位于第一个
If
块(在其下方)的外部,或者在第一个
While
循环之后应该有第二个
If

编辑
看起来这个国家就像一个阵列。您可能应该使用
计数器>=Country.Length

VB.NET中的数组是基于0的。这意味着第一个项目位于
国家(0)
,第二个项目位于
国家(1)
,等等。如果数组中有100个元素,则最后一个元素位于
国家(99)
<代码>国家/地区(100)不存在,如果您尝试访问它,将导致异常

我不确定您的家庭作业要求是什么,但通常要迭代集合的元素(数组、列表等),您会使用For循环。您可以使用Exit命令尽早从循环中放弃

For Counter As Integer = 0 To Country.Length - 1
     '...Country(Counter)
     If Found Then Exit For
Next

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

问题是它没有找到您希望找到的东西吗?我只需要加载一个较小的文件,然后用调试器逐步检查它,看看为什么它不匹配。搜索代码可能是正确的,因为您可以输入值并找到它们。加载/解析部分可能在标记中留下了前导空格、逗号或其他内容。我已将代码块移到if语句之外,但它仍然不起作用。如果我输入有效数据,一切正常,因为它不必跳转到If not语句。但是,如果输入无效数据,它会向“if Country(Counter).Names.ToUpper=txtCountry.Text.ToUpper-Then”行抛出异常,但会停止程序,而不是跳转到if-not-found语句
For Counter As Integer = 0 To Country.Length - 1
     '...Country(Counter)
     If Found Then Exit For
Next