当我运行此代码时,我收到此错误;从字符串""转换输入'整数'无效

时间:2014-04-20 18:32:05

标签: vb.net string integer

它突出显示 ##### 包围的部分是正确的,但我没有看到错误。

Private Sub UIDeposit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UIDeposit.Click
    'Declear(NumberOfCoins)

    Dim depositCash(7) As Integer

    'coins as expessions 
    depositCash(COIN1P) = CInt(UIPence1.Text)
    depositCash(COIN2P) = CInt(UIPence2.Text)
    depositCash(COIN5P) = CInt(UIPence5.Text)
    depositCash(COIN10P) = CInt(UIPence10.Text)
    depositCash(COIN20P) = CInt(UIPence20.Text)
    depositCash(COIN50P) = CInt(UIPence50.Text)
    depositCash(COIN100P) = CInt(UIPound1.Text)
    **depositCash(COIN200P) = CInt(UIPound2.Text)**

    'Displays 
    SetCoinAmount("0")
    depositAmount(depositCash)
    DisplaySandingAmount()

    'Curser Should Focus
    UIPence1.Focus()
End Sub

1 个答案:

答案 0 :(得分:0)

如果为整数分配字符串值,则必须在分配之前将该字符串转换为整数。如果字符串不包含数字,则会导致错误。您可以使用isnumeric或integer.tryparse函数使用if语句测试字符串,以确保在将字符串(文本框中的值)分配给整数(depositCash)之前它是合法的数字。