TryParse
所有基本数据类型,都有一个TryParse方法,用于将其它类型的数据转换为本类型的数据。
语法
Type.TryParse(Value,Variant)
说明
Type:目标数据类型,例如Date、Integer、Long、Double等等。
Value:要进行转换的数据。
Variant: 用于存放转换结果的变量,变量的类型必须和Type指定的类型一致。
如果转换成功,则将转换结果存储在变量Variant中,并返回True,否则返回False。
例如:
Dim
s As string = "123.1"
Dim d As Double
Double.TryParse(s, d) '将变量s的内容转换为数值,并存放在变量d中
Output.Show(d + 100) '输出结果是223.1
再例如:
Dim
d As Date '变量d用于存储转换结果
If Date.TryParse("1999/12/31", d) Then '如果转换成功
Output.Show(d) '输出转换结果
Else
Output.Show("无效日期格式") '给出错误提示
End If