使用Do Until Loop时,索引数小于索引数组的维数

时间:2017-12-07 20:07:44

标签: arrays vb.net

Private Sub TxtCoin_TextChanged(sender As Object, e As EventArgs) Handles TxtCoin.TextChanged
    ' declare a six row six column array for coin value ranges
    Static dblCoins(,) As Double = {{"0", "10"},
        {"10.01", "50"},
        {"50.01", "100"},
        {"100.01", "500"},
        {"500.01", "5000"},
        {"5000.01", "1000000000
        "}}
    ' declare parellel array
    Static strMsgVal() As String =
        {"Cheap", "Average", "Above Average", "Valuable", "Very Valuable", "Extremely Valuabele"}

    ' display the coin value message
    Dim strMessage As String
    Dim dblCoinVal = dblCoins
    Dim strMessa = strMsgVal.ToString

    strMessage = TxtCoin.Text

    ' search dblCoins for the amount 
    ' continue searching until the end of
    ' the array or the amount is found

    Do Until dblCoinVal = dblCoins.Length OrElse
            strMessage = dblCoins(dblCoinVal)
        dblCoinVal += 1
    Loop


    If strMessa = dblCoinVal.ToString Then
        lblCoinDescriptions.Text = strMessa.ToString
    Else
        MessageBox.Show("Please enter an integer")
    End If

当用户在我的数组的文本框范围内输入某个值并且收到错误时,尝试让我的标签显示一条消息。

Visual Basic

1 个答案:

答案 0 :(得分:1)

不要将代码放在TextedChanged事件中,因为每次用户输入一个数字时它都会运行。

Private Sub butOK_Click(sender As Object, e As EventArgs) Handles butOK.Click
        Dim dblCoins(,) As Double = {{0, 10},
        {10.0, 50},
        {50.0, 100},
        {100.0, 500},
        {500.0, 5000},
        {5000.0, 1000000000}}
        Dim strMsgVal() As String =
       {"Cheap", "Average", "Above Average", "Valuable", "Very Valuable", "Extremely Valuabele"}
        Dim x As Double
        If Double.TryParse(txtCoin.Text, x) Then
            For i As Integer = 0 To 5
                If x > dblCoins(i, 0) And x <= dblCoins(i, 1) Then
                    MessageBox.Show($"The value of {x:c} has the Apprasal Value of {strMsgVal(i)}")
                    Exit Sub
                End If
            Next
            MessageBox.Show("Cannot evaluate a negative number or 0.")
        Else
            MessageBox.Show("Please enter a number between 0 and 1000000000 without any commas or dollar signs.")
        End If
    End Sub

我认为这段代码强制数组是并行的。如果这有助于你,请标记为已回答。