[英]Error: Conversion from string “” to type 'Integer' is not valid
Select Case dlg
Case Windows.Forms.DialogResult.Yes
If TextBox12.Text = "" Then
Dim a, b, c As Integer
a = TextBox7.Text
b = TextBox8.Text //my problem
c = TextBox11.Text
TextBox12.Text = a + b - c
End If
If TextBox6.Text = "" Then
TextBox6.Text = "-"
End If
我不知道如何解決此錯誤:
從字符串“”到類型“ Integer”的轉換無效
嘗試使用Integer.Parse:
Select Case dlg
Case Windows.Forms.DialogResult.Yes
If TextBox12.Text = "" Then
Dim a, b, c As Integer
a = Integer.Parse(TextBox7.Text)
b = Integer.Parse(TextBox8.Text) //my problem
c = Integer.Parse(TextBox11.Text)
TextBox12.Text = a + b - c
End If
If TextBox6.Text = "" Then
TextBox6.Text = "-"
End If
If Not String.IsNullOrEmpty(TextBox8.Text) Then
b = Integer.Parse(TextBox8.Text)
End If
您可以檢查文本框是否為空或為空,然后使用int解析。
或者你可以使用使用Pikoh的建議Int32.TryParse
您應該看看使用Integer.TryParse 。 使用TryParse
的優點是,如果轉換失敗,它將不會引發異常:
Dim a As Integer = 0
Dim b As Integer = 0
Dim c As Integer = 0
Integer.TryParse(TextBox7.Text, a)
Integer.TryParse(TextBox8.Text, b)
Integer.TryParse(TextBox11.Text, c)
TextBox12.Text = (a + b - c).ToString()
您還應該查看將Option Strict On設置為:
將隱式數據類型轉換限制為僅擴大轉換,不允許后期綁定,並禁止導致對象類型的隱式類型。
從長遠來看,這將幫助您編寫更好的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.