Home
VB.NET
CStr Function
Updated Jan 11, 2025
Dot Net Perls
CStr. This VB.NET function converts literals into Strings—it handles Integer, Boolean and Char. It changes these types into their String representations.
Integer
Char
CType
ToString note. 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.
ToString
Example. 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.
Finally It converts the 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 Module
5 True a
Internals. 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.
Note The CStr("a"c) call above is optimized out by the Visual Basic .NET compiler for better performance.
Summary. 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.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Jan 11, 2025 (edit).
Home
Changes
© 2007-2025 Sam Allen