Section 16.2. Fundamentals of Characters and Strings


16.2. Fundamentals of Characters and Strings

Characters are the fundamental building blocks of Visual Basic source code. Every program is composed of characters that, when grouped together meaningfully, create a sequence that the compiler interprets as instructions describing how to accomplish a task. In addition to normal characters, a program also can contain character literals, also called character constants. A character literal is a character that is represented internally as an integer value, called a character code. For example, the integer value 97 corresponds to the character literal "a"c, and the integer value 122 corresponds to the character literal "z"c . The letter c following the closing double quote is Visual Basic's syntax for a character literal. Character literals are established according to the Unicode character set, an international character set that contains many more symbols and letters than the ASCII character set (listed in Appendix D). To learn more about Unicode, see Appendix E.

A string is a series of characters treated as a single unit. These characters can be uppercase letters, lowercase letters, digits and various special characters: +, -, *, /, $ and others. A string is an object of class String in the System namespace. We write string literals, also called string constants, as sequences of characters in double quotation marks, as follows:

 "John Q. Doe" "9999 Main Street" "Waltham, Massachusetts" "(201) 5551212" 


A declaration can assign a String literal to a String variable. The declaration

 Dim color As String = "blue" 


initializes String variable color to refer to the String literal object "blue".

Performance Tip 16.1

If there are multiple occurrences of the same String literal in an application, a single copy of the String literal object will be referenced from each location in the program that uses that String literal. It is possible to share the object in this manner because String literal objects are implicitly constant. Such sharing conserves memory.


Common Programming Error 16.1

Assigning Nothing to a String variable can lead to logic errors if you attempt to compare Nothing to an empty String. The keyword Nothing is a null reference, not an empty String (which is a String that is of length 0 and contains no characters).





Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net