Home

Visual Basic Built-In Functions: Conversions

   

Introduction

You may recall that when studying data types, we saw that each had a corresponding function used to convert a string value or an expression to that type. As a reminder, the general syntax of the conversion functions is:

ReturnType = FunctionName(Expression)

The Expression could be of any kind. For example, it could be a string or expression that would produce a value such as the result of a calculation. The conversion function would take such a value, string, or expression and attempt to convert it. If the conversion is successful, the function would return a new value that is of the type specified by the ReturnType in our syntax.

The conversion functions are as follows:

Function  
Name Return Type Description
CBool Boolean Converts an expression into a Boolean value
CByte Byte Converts an expression into Byte number
CDbl Double Converts an expression into a floating-point number with double precision
CDec Decimal Converts an expression into a decimal number
CInt Integer Converts an expression into an integer (natural) number
CLng Long Converts an expression into a long integer (a large natural) number
CObj Object Converts an expression into an Object type
CSByte SByte Converts an expression into a signed byte
CShort Short Converts an expression into a short integer
CSng Single Converts an expression into a floating-point number with single precision
CUInt UInt Converts an expression into an unsigned integer
CULng ULong Converts an expression into an unsigned long integer
CUShort UShort Converts an expression into an unsigned short integer

Type Conversion 

Conversion functions allow you to convert a known value to a another type. Besides these functions, the Visual Basic language provides a function named CType. Its syntax is:

CType(expression, typename)

As you can see, the CType() function takes two arguments. The first argument is the expression or the value that you want to convert. An example could be name of a variable or a calculation:

CType(250.48 * 14.05, ...)

The second argument is the type of value you want to convert the first argument to. From what have learned so far, this second argument can be one of the data types we reviewed in Lesson 3. Here is an example:

CType(250.48 * 14.05, Single)

If you choose one of the Visual Basic language's data types, the expression produced by the first argument must be able to produce a value that is conform to the type of the second argument:

  • The conversion from the first argument to the type of the second argument must be possible: the value produced by the first must be convertible to the second arguments. For example, if the first argument is a calculation, the second argument must be a number-based data type. In the same way, you cannot convert a date to a number-based type
  • If the first argument is a number or the result of a calculation, its resulting value must be lower than or up to the range of values of the second argument. Here is an example:
     
    Public Module Exercise
    
        Public Function Main() As Integer
    
            MsgBox(CType(250.48 * 14.05, Single))
    
            Return 0
        End Function
    
    End Module

    This would produce:

    Conversion

  • If the first argument is a number or the result of a calculation that produces an integer or a floating-point number, its resulting value must be convertible to an integer or a floating point number up to the range of values of the second argument. Here is an example:
     
    Public Module Exercise
    
        Public Function Main() As Integer
    
            MsgBox(CType(7942.225 * 202.46, UInteger))
    
            Return 0
        End Function
    
    End Module

    This would produce:

    Conversion

  • If the first argument is a number or the result of a calculation that produces an integer or a floating-point number, the second argument is a number-based data type but whose range cannot hold the resulting value of the first argument, the conversion would not be allowed (the conversion will fail):
     

    Conversion

After the CType() function has performed its conversion, it returns a value that is the same category as the second argument. For example, you can call a CType() function that converts an expression to a long integer. Here is an example:

Public Module Exercise

    Public Function Main() As Integer
        Dim Number As Long

        Number = CType(7942.225 * 202.46, Long)

        Return 0
    End Function

End Module

The function can also return a different type, as long as its type can hold the value produced by the expression. Here are two examples:

Public Module Exercise

    Public Function Main() As Integer
        Dim Number As UInteger

        Number = CType(7942.225 * 202.46, Long)

        Return 0
    End Function

End Module

Or

Public Module Exercise

    Public Function Main() As Integer
        Dim Number As Single

        Number = CType(7942.225 * 202.46, Long)

        Return 0
    End Function

End Module

If you try storing the returned value into a variable that cannot hold it, you would receive an error:

Conversion

 

 
 
     
 

Home Copyright © 2008-2016, FunctionX, Inc.