Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TryCast to Double?

Tags:

casting

vb.net

I am trying to cast a string to double, but I don't want it to throw exception, but to return some value. I found the TryCast function but it seems I am not using it right. I try TryCast(string, Double) and it says that "Double" is a value type, not a reference type. What am I doing wrong?

like image 666
barakuda28 Avatar asked Oct 25 '12 23:10

barakuda28


People also ask

What does the Double TryParse do?

TryParse(String, Double) Converts the string representation of a number to its double-precision floating-point number equivalent. A return value indicates whether the conversion succeeded or failed.

What is TryCast?

TryCast returns Nothing, so that instead of having to handle a possible exception, you need only test the returned result against Nothing . You use the TryCast keyword the same way you use the CType Function and the DirectCast Operator keyword.


3 Answers

Double.TryParse(string, double)

like image 176
snoopy-do Avatar answered Oct 23 '22 07:10

snoopy-do


You should use the TryParse method instead.

like image 28
Sani Singh Huttunen Avatar answered Oct 23 '22 07:10

Sani Singh Huttunen


You should try this:

If Double.TryParse(value, New Double) Then
    ' TODO
end if
like image 34
Arnaud DHENRY Avatar answered Oct 23 '22 09:10

Arnaud DHENRY