嗨,我想问一下
如果Val(Textbox1.Text)用于保存整数值。 在持有String ??
的值时应该放什么答案 0 :(得分:1)
Textbox1.Text
是一个字符串,因此一个简单的字符串变量将起作用:
Dim s As String = Textbox1.Text
但是,请注意Val
没有"持有"值,但是将字符串转换为值的函数。它始终返回Double
,而不是Integer
。
要将字符串转换为Integer
,请使用Convert.ToInt32
或CInt
。使用TextBoxes虽然用户可能会输入"123foo45"
等非法数据,但您应该测试内容以避免错误:
Dim n As Integer
If Integer.TryParse(TextBox1.Text, n) Then
' text can parse, n holds the value
Else
' tell the user they entered bad info
End If