Data Conversion in Visual Basic .NET

In this article, I will explain you about Data Conversion in Visual Basic .NET.
  • 3166

In this article, I will explain you about Data Conversion in Visual Basic .NET.

Data Conversion


In Visual Basic .NET, when we need to convert the data type of a variable as the program proceeds, we use data conversion to convert one data type to another. We have two ways to convert data implicitly and explicitly.

Implicit Conversion

Implicit conversion is referred to the conversion that performed automatically. In Implicit conversion compiler convert one data type to another itself. The code below will help you to understand implicit conversion more clearly:

Example:

Imports System.Console
Module Module1
 
    Sub Main()
        Dim A As Double
        'Declare a variable of Double data type named A
        A = 12211.12211
        Dim B As Integer
        'Declare another variable of Integer data type named B
        B = A
       
'implicit conversion
        WriteLine("The value in Integer is " & B)
        Read()
    End Sub

End Module

Output:

Output4.gif

Explicit Conversion

Explicit conversion is referred to the conversion that does not performed automatically, we have to perform the conversion. It also known as casting. CType function is use to accomplished Explicit conversion. The code below will help you to understand Explicit conversion more clearly:

Example:

Imports System.Console
Module Module1
 
    Sub Main()
        Dim A As Double
        A = 12211.12211
        Dim B As Integer
        B = CType(A, B)
        'two arguments, type we are converting from, to type desired
        WriteLine("The value in Integer is " & B)
        Read()
    End Sub
 
End Module 

Output:

Output5.gif

The list of conversion functions which we can use in Visual Basic .NET.

Functions

Use

CInt

This function convert to Integer data type.

CChar

This function convert to Char data type.

CString

This function to convert  String data type.

CShort

This function convert to Short data type.

CDbl

This function convert to Double data type.

CLng

This function convert to Long data type.

CDec

This function convert to Decimal data type.

CBool

This function convert to Bool data type.

CByte

This function convert to Byte data type.

CDate

This function  convert to Date type.

CObj

This function convert to Object type.

CSng

This function convert to Single data type.

Summary

I hope this article help you to understand Data Conversion in Visual Basic .NET.
 

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.