ToByte Method (Object)

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.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Shared Function ToByte ( _
	value As Object _
) As Byte

Parameters

value
Type: System.Object
An Object that implements the IConvertible interface or Nothing.

Return Value

Type: System.Byte
An 8-bit unsigned integer equivalent to the value of value, or zero if value is Nothing.

ExceptionCondition
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


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft