Home »
VB.Net »
VB.Net Programs
VB.Net program to demonstrate the CStr() function
By Nidhi Last Updated : November 11, 2024
CStr() function in VB.Net
The CStr() function is used to convert different data types value into string type.
Syntax
CStr(val)
Parameter(s)
- val: It may be a variable of different data types.
Return Value
The CStr() function will return a converted string value.
VB.Net code to demonstrate the example of CStr() function
The source code to demonstrate the CStr() function is given below. The given program is compiled and executed successfully.
'VB.Net program to demonstrate the CStr() function.
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
Press any key to continue . . .
Explanation:
In the above program, we created a module Module1 that contains a Main() method. In the Main() method, we created five variables str, n1, n2, n3, and n4 that are initialized with 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)
In the above code, we converted the value of the specified variable into string value and printed them on the console screen.
VB.Net Basic Programs »