什么用于在vb.net中保存String值

时间:2014-09-30 13:31:56

标签: vb.net string int

嗨,我想问一下

如果Val(Textbox1.Text)用于保存整数值。 在持有String ??

的值时应该放什么

1 个答案:

答案 0 :(得分:1)

Textbox1.Text 一个字符串,因此一个简单的字符串变量将起作用:

Dim s As String = Textbox1.Text

但是,请注意Val没有"持有"值,但是将字符串转换为值的函数。它始终返回Double,而不是Integer

要将字符串转换为Integer,请使用Convert.ToInt32CInt。使用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