如何在没有它们聚集的情况下将变量添加到一起?

时间:2013-10-31 13:49:10

标签: vb.net-2010

我有一个包含以下代码的程序:

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?

2 个答案:

答案 0 :(得分:0)

你可以将它们中的每一个转换为int,求它们然后将它转换回字符串:

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

答案 1 :(得分: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.