Character variables store a single Unicode character in two bytes. In effect, characters are Unsigned Short integers (
UInt16); you can use the
CChar() function to convert integers to characters and use the
CInt() function to convert characters to their equivalent integer values.
To declare a Character variable, use the
Dim char1, char2 As CharCode language: VB.NET (vbnet)
You can initialize a Character variable by assigning either a character or a string to it. In the latter case, only the first character of the string is assigned to the variable. The following statements will print the characters a and A to the Output window:
Dim char1 As Char = "a", char2 As Char = "ABC"
Debug.WriteLine(char2)Code language: VB.NET (vbnet)
These statements will work only if the Strict option is off. If it’s on, the values assigned to the char1 and char2 variables will be marked in error. To fix the error that prevents the compilation of the code, change the Dim statement as follows:
Dim char1 As Char = "a"c, char2 As Char = "A"cCode language: VB.NET (vbnet)
When the Strict option is on, you can’t assign a string to a Char variable and expect that only the first character of the string will be used.
The Integer values that correspond to the English characters are the ANSI (American National Standards Institute) codes of the equivalent characters. The following statement will print the value 65:
Debug.WriteLine(Convert.ToInt32("a"))Code language: VB.NET (vbnet)
If you convert the Greek character alpha (
α) to an integer, its value is 945. The Unicode value of the famous character π is 960.
Character variables are used in conjunction with strings. You’ll rarely save real data as characters. However, you might have to process the individual characters in a string, one at a time.
The Char data type exposes a number of interesting methods for manipulating characters, and they’re presented in detail in Chapter “Handling Strings, Characters, and Dates”. Let’s say the string variable password holds a user’s new password, and you require that passwords contain at least one special symbol. The code segment of Listing 2.3 scans the password and rejects it if it contains letters and digits only.
Listing 2.3: Processing Individual Characters
Dim password As String, ch As Char
Dim i As Integer
Dim valid As Boolean = False
While Not valid
password = InputBox("Please enter your password")
For i = 0 To password.Length - 1
ch = password.Chars(i)
If Not Char.IsLetterOrDigit(ch) Then
valid = True
If valid Then
MsgBox("You new password will be activated immediately!")
MsgBox("Your password must contain at least one special symbol!")
End WhileCode language: VB.NET (vbnet)
If you are not familiar with the
If. . .Then,
For. . .Next, or
While. . .End While structures, you can read their descriptions in the following chapter.
The code prompts the user with an input box to enter a password. The valid variable is Boolean and it’s initialized to False. (You don’t have to initialize a Boolean variable to False because this is its default initial value, but it does make the code easier to read.) It’s set to True from within the body of the loop, only if the password contains a character that is not a letter or a digit. We set it to False initially, so the
While. . .End While loop will be executed at least once. This loop will keep prompting the user until a valid password is entered.
For. . .Next loop scans the string variable password, one letter at a time. At each iteration, the next letter is copied into the ch variable. The Chars property of the String data type is an array that holds the individual characters in the string (another example of the functionality built into the data types).
Then the program examines the current character. The IsLetterOrDigit method of the Char data type returns True if a character is either a letter or a digit. If the current character is a symbol, the program sets the valid variable to True so that the outer loop won’t be executed again, and it exits the For. . .Next loop. Finally, it prints the appropriate message, and either prompts for another password or quits.
Char class and its methods are discussed in more detail in Chapter “Handling Strings, Characters, and Dates”.