CByte Function

   
CByte Function

Named Arguments

No

Syntax

 CByte(   expression   ) 
expression (required; String or Numeric)

A string or numeric expression that evaluates to a number between 0 and 255

Return Value

expression converted to Byte data type

Description

Converts expression to a Byte data type

Rules at a Glance

  • If the expression to be converted is a string, the string must be capable of conversion to a numeric expression; this can be checked using the IsNumeric function.

  • If expression evaluates to less than 0 or more than 255, a runtime error is generated.

  • If the value of expression is not a whole number, CByte rounds the number prior to conversion.

Example

 If IsNumeric(sMyNumber) Then     If val(sMyNumber) >= 0 and val(sMyNumber) <= 255 Then         BytMyNumber = CByte(sMyNumber)     End If End If 

Programming Tips and Gotchas

  • Check that the value you pass to CByte is neither negative nor greater than 255.

  • Use IsNumeric to ensure that the value passed to CByte can be converted to a numeric expression.

  • When using CByte to convert floating point numbers , fractional values up to but not including .5 are rounded down, while values above but not including .5 are rounded up. Values whose fractional component is exactly equal to .5 are rounded up if their integral component is odd and down if their integral component is even.

  • The CByte function converts an expression to an unsigned byte data type. To convert expression to a signed byte data type, create an instance of the SByte class and call its Parse method.

  • Like most of the conversion functions, CByte is not actually a function in the Microsoft.VisualBasic namespace. Instead, it is similar to a Visual C++ macro; the compiler translates the function call into inline code.

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net