I don't like having to do something like this every time I use CInt
or Integer.Parse
(which I realize is costly):
Try
someIntVariable = CInt(someStringVariable)
Catch ex As Exception
someIntVariable = 0
End Try
And I don't like all the lines of code involved with Integer.TryParse
. I guess I could create a function that does something like:
Function ToInteger(str As String) As Integer
Dim number As Integer
Int32.TryParse(str, number)
Return number
End Function
But isn't there already an extension method or something built into .NET that can do this for me in a more elegant way (along the lines of Nullable(Of T).GetValueOrDefault
)?