KG Sosa - 25 days ago 6

Vb.net Question

Basically, I have been using both

`Integer.Parse`

What is the difference between

`Integer.Parse`

`CInt`

Answer Source

`CInt`

does a whole lot more than `Integer.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 Functions

Fractional Parts. When you convert a nonintegral value to an integral type, the integer conversion functions (CByte, CInt, CLng, CSByte, CShort, CUInt, CULng, and CUShort) remove the fractional part and round the value to the closest integer.

If the fractional part is exactly 0.5, the integer conversion functions round it to the nearest even integer. For example, 0.5 rounds to 0, and 1.5 and 2.5 both round to 2. This is sometimes called banker's rounding, and its purpose is to compensate for a bias that could accumulate when adding many such numbers together.

So 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`

(or `Integer.TryParse`

), if you're coercing an unknown value (e.g. a `variant`

or `object`

from a database) to an integer, use `CInt`

.