Home >>VB.Net Built-in Functions >VB.Net program to demonstrate the CStr() function

VB.Net program to demonstrate the CStr() function

Write a VB.Net program to demonstrate the CStr() function

The function CStr() is used to convert the value of different data types into a string type.

Syntax :

CStr(val)

Parameters :

  • val: It may be a variable with different types of data.

Return Value :

A converted string value will be returned by the CStr() function.

Program :

Below is the source code for demonstrating the CStr() function. The program given is compiled and successfully executed.


Module Module1
    Sub Main()
        Dim str As String = ""
        Dim n1 As Single = 10.25
        Dim n2 As Integer = 12
        Dim n3 As Double = 25.35
        Dim n4 As String = "122"
        str = CStr(n1)
        Console.WriteLine("String value: {0}", str)
        str = CStr(n2)
        Console.WriteLine("String value: {0}", str)
        str = CStr(n3)
        Console.WriteLine("String value: {0}", str)
        str = CStr(n4)
        Console.WriteLine("String value: {0}", str)
    End Sub
End Module
	
Output:
String value: 10.25
String value: 12
String value: 25.35
String value: 122

Explanation:

We created a Module1 module in the program above that contains the Main() method. We have created five str, n1, n2, n3, and n4 variables in the Main() method that are initialized with a blank string, 10.25, 12, 25.35, and 122.

str = CStr(n1)
Console.WriteLine("String value: {0}", str)

str = CStr(n2)
Console.WriteLine("String value: {0}", str)

str = CStr(n3)
Console.WriteLine("String value: {0}", str)

str = CStr(n4)
Console.WriteLine("String value: {0}", str)

We converted the value of the specified variable to a string value in the code above and printed it on the console screen.


No Sidebar ads