共用方式為


Char Data Type (Visual Basic) 

Holds unsigned 16-bit (2-byte) code points ranging in value from 0 through 65535. Each code point, or character code, represents a single Unicode character.

Remarks

Use the Char data type when you need to hold only a single character and do not need the overhead of String. In some cases you can use Char(), an array of Char elements, to hold multiple characters.

The default value of Char is the character with a code point of 0.

Unicode Characters

The first 128 code points (0–127) of Unicode correspond to the letters and symbols on a standard U.S. keyboard. These first 128 code points are the same as those the ASCII character set defines. The second 128 code points (128–255) represent special characters, such as Latin-based alphabet letters, accents, currency symbols, and fractions. Unicode uses the remaining code points (256-65535) for a wide variety of symbols, including worldwide textual characters, diacritics, and mathematical and technical symbols.

You can use methods like IsDigit and IsPunctuation on a Char variable to determine its Unicode classification.

Type Conversions

Visual Basic does not convert directly between Char and the numeric types. You can use the Asc, AscW Functions to convert a Char value to an Integer representing its code point. You can use the Chr, ChrW Functions to convert an Integer value to a Char having that code point.

If the type checking switch (Option Strict Statement) is on, you must append the literal type character to a single-character string literal to identify it as the Char data type. The following example illustrates this.

Option Strict On
Dim charVar As Char
' The following statement attempts to convert a String literal to Char.
' Because Option Strict is On, it generates a compiler error.
charVar = "Z"
' The following statement succeeds because it specifies a Char literal.
charVar = "Z"C

Programming Tips

  • Negative Numbers. Char is an unsigned type and cannot represent a negative value. In any case, you should not use Char to hold numeric values.

  • Interop Considerations. If you are interfacing with components not written for the .NET Framework, for example Automation or COM objects, keep in mind that character types have a different data width (8 bits) in other environments. If you are passing an 8-bit argument to such a component, declare it as Byte instead of Char in your new Visual Basic code.

  • Widening. The Char data type widens to String. This means you can convert Char to String without encountering a System.OverflowException error.

  • Type Characters. Appending the literal type character C to a single-character string literal forces it to the Char data type. Char has no identifier type character.

  • Framework Type. The corresponding type in the .NET Framework is the System.Char structure.

See Also

Tasks

How to: Optimize Storage of Positive Integers With Unsigned Types
How to: Call a Windows Function that Takes Unsigned Types

Reference

Data Type Summary (Visual Basic)
String Data Type (Visual Basic)
Type Conversion Functions
Conversion Summary
Asc, AscW Functions
Chr, ChrW Functions
System.Char

Concepts

Efficient Use of Data Types