Data Type Conversion in Visual Basic .Net


Visual Basic .Net does not convert the variable data types from one to another, when you assign a value of one type to a variable of another. Instead of this VB.Net provides some functions to do the conversion between datatypes.

You may already learned the basic datatypes used in Visual Basic .Net, here we are learning the conversion between datatypes.

The following functions are provided by Visual Basic to do the datatype conversion:

  • CBool : Convert to Bool data type.
  • CByte : Convert to Byte data type.
  • CChar : Convert to Char data type.
  • CDate : Convert to Date data type.
  • CBbl : Convert to Double data type.
  • CDec : Convert to Decimal data type.
  • CInt : Convert to Integer data type.
  • CLng : Convert to Long data type.
  • CObj : Convert to Object type.
  • CShort : Convert to Short data type.
  • CSng : Convert to Single data type.
  • CStr : Convert to String data type.

Example Program:

The below program will give you more explanation about data type conversion in Visual Basic .Net.

Module Module1

  Sub Main()
    Dim Int_Val As Integer
    Dim Dbl_Val As Integer
    Dbl_Val = 3.14
    Int_Val = CInt(Dbl_Val)

  End Sub

End Module

The output of the given program will be 3. Here the Int_Val can hold only the integer values, so we converted the double value to the integer while assigning the value.