0

I have a program with the following code:

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

Lets assume the user enters 1, 2, and 3 for the three textbox variables, I want EndVar to equal 6, but it gives me 123. How do I get it to give me 6?

4

2 回答 2

0

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

   Dim EndVar as string = (Convert.toInt32(Var1) + Convert.toInt32(Var2) +   Convert.toInt32(Var3)).ToString();
于 2013-10-31T13:52:52.800 回答
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.
于 2013-10-31T13:53:50.630 回答