Data Conversions in Visual Basic.

In many cases, Visual Basic is able to perform automatic data conversions on the data it executed during runtime, but certain times when the automatic conversion of data is impossible, especially in cases which involve conversion of dates, a manual conversion is required.

To simulate an example on how visual basic can identify and converts data automatically, take a look at the simple codes below.

Dim myData As Double '---Variables declaration

Dim CvtData As Integer

myData = 9800.4523 '--- store data into myData variable as data type "Double"

CvtData = myData '----transfer the data from myData into CvtData

MsgBox(CvtData) '--- Output data becomes 9800

The simple code above illustrates that the Visual Basic has automatically converted the data from Double into Integer data type. Now take a look at the second sample code below.

Dim CvtData as Integer '---Variables declaration

Dim TextData As String

CvtData = 9800 '--- store previous examples data

TextData = CvtData '----transfer the data from CvtData into TextData

MsgBox(TextData) '--- Now the output data '9800' has become a string

Like the first example, Visual Basic has again automatically converted the data from Integer into String data type. If Visual Basic is unable to automatically convert the data, then a manual data conversion can be done by using the built-in Cast (CType) function.

See How To Perform Manual Data Conversion Using CType Function

0 comments