2013-10-31 42 views
0

我有一個程序,用下面的代碼:如何將變量添加到一起而不會聚攏起來?

Dim Var1 as string = textbox1.text 

Dim Var2 as string = textbox2.text 

Dim Var3 as string = textbox3.text 

Dim EndVar as string = Var1 + Var2 + Var3 

讓我們假設用戶三個文本框變量輸入1,2,3,我想EndVar等於6,但它給了我123怎麼辦我得到它給我6?

回答

0

您可以將他們每個人詮釋,總結它們,然後將其轉換回字符串:

Dim EndVar as string = (Convert.toInt32(Var1) + Convert.toInt32(Var2) + Convert.toInt32(Var3)).ToString(); 
+0

工作就像一個魅力,開箱即用。謝謝!。 – Jayleaf

0

System.Convert.To ......你需要什麼... 看看這個http://msdn.microsoft.com/en-us/library/sf1aw27b.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2

Dim values() As String = { "One", "1.34e28", "-26.87", "-18", "-6.00", _ 
          " 0", "137", "1601.9", Int32.MaxValue.ToString() } 
Dim result As Integer 

For Each value As String In values 
    Try 
     result = Convert.ToInt32(value) 
     Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", _ 
         value.GetType().Name, value, result.GetType().Name, result) 
    Catch e As OverflowException 
     Console.WriteLine("{0} is outside the range of the Int32 type.", value) 
    Catch e As FormatException 
     Console.WriteLine("The {0} value '{1}' is not in a recognizable format.", _ 
         value.GetType().Name, value) 
    End Try  
Next         
' The example displays the following output: 
' The String value 'One' is not in a recognizable format. 
' The String value '1.34e28' is not in a recognizable format. 
' The String value '-26.87' is not in a recognizable format. 
' Converted the String value '-18' to the Int32 value -18. 
' The String value '-6.00' is not in a recognizable format. 
' Converted the String value ' 0' to the Int32 value 0. 
' Converted the String value '137' to the Int32 value 137. 
' The String value '1601.9' is not in a recognizable format. 
' Converted the String value '2147483647' to the Int32 value 2147483647. 
相關問題