Basically, I have been using both Integer.Parse
and CInt in most of my daily programming tasks, but I'm a little bit confused of what the difference is between the two.
What is the difference between Integer.Parse
and CInt
in VB.NET?
vb.net
Basically, I have been using both Integer.Parse
and CInt in most of my daily programming tasks, but I'm a little bit confused of what the difference is between the two.
What is the difference between Integer.Parse
and CInt
in VB.NET?
Best Answer
CInt
does a whole lot more thanInteger.Parse
.CInt
will first check to see if what it was passed is an integer, and then simply casts it and returns it. If it's a double it will try to convert it without first converting the double to a string.See this from the help for
CInt
and other Type Conversion FunctionsSo in short, it does much more than convert a string to an integer, e.g. applying specific rounding rules to fractions, short circuting unecessary conversions etc.
If what you're doing is converting a string to an integer, use
Integer.Parse
(orInteger.TryParse
), if you're coercing an unknown value (e.g. avariant
orobject
from a database) to an integer, useCInt
.