我是Visual Basic中的新手,我正在尝试做一些简单的事情,但是当我试图解析一个字符串加倍时我遇到了困难,我已经重新填充并且有效地问题是方法Double.TryParse因为总是返回-1作为转换后的double。如果有人指出我做错了什么,我将不胜感激。
这是调用解析方法的类
Public Class Sumar
Dim utilerias As Utilerias
Public Function realizarSuma(ByVal Snum1 As String, ByVal Snum2 As String) As Double
utilerias = New Utilerias()
Dim num1, num2 As Double
num1 = utilerias.parseStringToDouble(Snum1)
num2 = utilerias.parseStringToDouble(Snum2)
Return num1 + num2
End Function
End Class
这是解析
的方法Public Function parseStringToDouble(ByVal Snum As String) As Double
Dim num As Double
num = Double.TryParse(Snum, num)
Return num
End Function
答案 0 :(得分:2)
Double.TryParse()
的返回值表示解析是否有效,它是一个布尔值。在您的调用中,您将使用返回值覆盖结果(在引用传递的第二个参数中)。你会想要这样的东西:
Public Function parseStringToDouble(ByVal Snum As String) As Double
Dim num As Double
If Not Double.TryParse(Snum, num) Then
' Report error. Input was not a number
Return num
End Function
PS:对不起,我不再那么流利的VB语法,但你会得到这个概念...