Convert.ToByte Method (Object)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Converts the value of the specified Object to an 8-bit unsigned integer.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- value
- Type: System.Object
An Object that implements the IConvertible interface or Nothing.
Return Value
Type: System.ByteAn 8-bit unsigned integer equivalent to the value of value, or zero if value is Nothing.
| Exception | Condition |
|---|---|
| InvalidCastException | value does not implement IConvertible. |
If value is not Nothing, this method wraps a call to the IConvertible.ToByte implementation of the underlying type of value.
The following code sample illustrates the use of ToByte, converting a String value to a Byte :
Public Sub ConvertStringByte(ByVal stringVal As String) Dim byteVal As Byte = 0 Try byteVal = System.Convert.ToByte(stringVal) outputBlock.Text &= String.Format("{0} as a byte is: {1}", _ stringVal, byteVal) & vbCrLf Catch exception As System.OverflowException outputBlock.Text &= String.Format( _ "Overflow in string-to-byte conversion.") & vbCrLf Catch exception As System.FormatException outputBlock.Text &= String.Format( _ "The String is not formatted as a Byte.") & vbCrLf Catch exception As System.ArgumentException outputBlock.Text &= "The String is null." & vbCrLf End Try 'The conversion from byte to string is always valid. stringVal = System.Convert.ToString(byteVal) outputBlock.Text &= String.Format("{0} as a string is {1}", _ byteVal, stringVal) & vbCrLf End Sub
Show: