This VB.NET function converts literals into Strings—it handles Integer, Boolean
and Char
. It changes these types into their String
representations.
In VB.NET, we can call ToString
, which is more like how things work in the C# language. Often, using ToString
is preferred as it is more standard.
This program calls the CStr function and passes 5 as the argument. It shows that you can convert the Boolean
True into the String
"True" by using the CStr function.
Char
(a) into the String
"a". It uses the CStr function for this as well.Module Module1 Sub Main() ' Convert integer to string. Dim str As String = CStr(5) If str = "5" Then Console.WriteLine(str) End If ' Convert bool to string. str = CStr(True) If str = "True" Then Console.WriteLine(str) End If ' Convert char to string. str = CStr("a"c) If str = "a" Then Console.WriteLine(str) End If End Sub End Module5 True a
How does the .NET Framework implement the CStr function? It is compiled CStr into the Conversions.ToString
function. This eventually returns the correct String
value.
The CStr function provides a way to effectively convert value types into String
types. It is useful—you cannot call ToString
on literals in the VB.NET environment.