|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
Chr, ChrW Functions
Returns the character associated with the specified character code.
Public Function Chr(ByVal CharCode As Integer) As Char Public Function ChrW(ByVal CharCode As Integer) As Char
Required. An Integer expression representing the code point, or character code, for the character. If CharCode is outside the valid range, an error occurs. The valid range for Chr is 0 through 255, and the valid range for ChrW is -32768 through 65535.
|Exception type||Error number||Condition|
CharCode < -32768 or > 65535 for ChrW.
CharCode < 0 or > 255 for Chr.
See the "Error number" column if you are upgrading Visual Basic 6.0 applications that use unstructured error handling. (You can compare the error number against the.) However, when possible, you should consider replacing such error control with .
The asymmetric range accepted for CharCode compensates for the storage differences between the and the . For example, -29183 is a Short but +36353 is an Integer. This also facilitates compatibility with Visual Basic 6.0.
Chr uses the CharCode as a code point in the appropriate set. The range can be 0 through 255 for SBCS characters and -32768 through 65535 for DBCS characters. For charts of the single-byte ASCII characters, see .class in the namespace to determine if the current thread is using a single-byte character set (SBCS) or a double-byte character set (DBCS). It then takes
The returned value depends on the code page for the current thread, which is contained in theproperty of the class in the namespace. You can obtain ANSICodePage by specifying System.Globalization.CultureInfo.CurrentCulture.TextInfo.ANSICodePage.
ChrW takes CharCode as a Unicode code point. The range is independent of the culture and code page settings for the current thread. Values from -32768 through -1 are treated the same as values in the range +32768 through +65535.
Numbers from 0 through 31 are the same as standard nonprintable ASCII codes. For example, Chr(10) returns a line feed character.
The ChrB function in earlier versions of Visual Basic returns a single byte. It is used primarily for converting strings in double-byte character set (DBCS) applications. All strings in Visual Basic and the .NET Framework are in Unicode, and ChrB is no longer supported.
The following example uses the Chr function to return the character associated with the specified character code.
Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)