Bagikan melalui


How to: Hold Characters in a Variable

A variable holds individual characters if you declare it to be of data type Char or String.

A variable of the Char Data Type (Visual Basic) can hold a single Unicode character. A variable of the String Data Type (Visual Basic) can hold a sequence of zero or more Unicode characters.

Unicode Characters. Each possible value in a Char or String variable is a code point, or character code, in the Unicode character set. Unicode characters include the basic ASCII character set, various other alphabet letters, accents, currency symbols, fractions, diacritics, and mathematical and technical symbols.

If you never need to hold more than one character in a single variable, use the Char data type. The String data type requires more memory and has slower performance.

Note

The Unicode character set reserves the code points D800 through DFFF (55296 through 55551 decimal) for surrogate pairs, which require two 16-bit values to represent a single code point. A Char variable cannot hold a surrogate pair, and a String uses two positions to hold such a pair.

To hold a single character in a variable

  1. Declare the variable with a Dim Statement (Visual Basic).

  2. Follow the variable name with an As clause.

  3. Follow the As keyword with the Char keyword.

To hold a sequence of characters in a variable

  1. Declare the variable with a Dim statement.

  2. Follow the variable name with an As clause.

  3. Follow the As keyword with the String keyword.

See Also

Concepts

Data Types in Visual Basic

Type Characters

Reference

Data Type Summary (Visual Basic)

Single Data Type (Visual Basic)

Char Data Type (Visual Basic)

String Data Type (Visual Basic)

Other Resources

Elementary Data Types