Mega Code Archive

 
Categories / VB.Net / Development
 

Convert ToByte (Object) converts object to an 8-bit unsigned integer

Class Sample    Public Shared Sub Main()         Dim values() As Object = { True, -12, 935, "x"c, "104.1", "-1","1.00e2", "One", 1.00e2}         Dim result As Byte         For Each value As Object In values            Try               result = Convert.ToByte(value)               Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.", _                                 value.GetType().Name, value, _                                 result.GetType().Name, result)            Catch e As OverflowException               Console.WriteLine("The {0} value {1} is outside the range of the Byte type.", _                                 value.GetType().Name, value)            Catch e As FormatException               Console.WriteLine("The {0} value {1} is not in a recognizable format.", _                                 value.GetType().Name, value)            Catch e As InvalidCastException               Console.WriteLine("No conversion to a Byte exists for the {0} value {1}.", _                                 value.GetType().Name, value)                     End Try         Next                                End Sub End Class