Data Conversion in VB


To manually convert data in Visual basic, we can use the in-built function CType to perform the task. CType function is used to perform conversion of any value from one data type into another data type. However, if the value that you intend to convert into and the data type is outside of the value range allowed then an error might occur. The syntax for CType is show as follows

CType(Expression, NameOfDataType)


The NameOfDataType can be any expression that is valid within As clause in a Dim statement, name of any datatype, class, object structure or even interface. The example below shows how CType can be used in VB programming.


Dim myString As String '--- Variable Declaration
Dim myNumber As Integer

myString ="5469" '---Initialize string

myNumber = CType(myString, Integer) '--- convert string to integer

MsgBox(myNumber) '--- Result myNumber will be integer 5469


CType function is rather similar to the other functions used in VB6 like the ones listed below, These functions are used to convert data into the following data type as listed on the right side.


CBool - Boolean

CByte - Byte

CChar - Char

CDate - Date

CDbl - Double

CDec - Decimal

CInt - Integer

CLng - Long

CSng - Single

CStr - String


Hence, that shows how data conversion can be done manually by using CType functions.

0 comments