Conversions - Parse
What are Conversions?
When Option Strict is turned on, Visual Basic .NET is a type safe programming language. This means that Visual Basic .NET will validate data types and throw an error if you try to assign the wrong type to a variable.
When Option Strict is turned off, Visual Basic .NET allows for implicit data conversions. This means that you can convert a Double (number with a decimal) to an Integer (number without a decimal) without explicitly telling the Visual Basic .NET compiler what to do with the no longer needed decimal place.
Parse Methods
The least effective method to convert data is to use the data type's Parse method.
The Parse method will attempt to convert a value to a designated data type, but if it fails, an exception is thrown.
The following demonstrates how to convert a String to an Integer using the Integer's Parse method:
Fiddle: Live Demo
ToString
The Parse method will only ever accept a String value. Fortunately, every object in Visual Basic has a ToString method that will return the String representation of that object.
The following demonstrates how to convert a Double to an Integer using the Integer's Parse method combined with the ToString method:
Fiddle: Live Demo
Exception
If the value to be converted fails the conversion, a FormatException will be thrown.
Remarks
The Parse method should only ever be used if you are absolutely certain that the value can be converted properly.