Section 16.14. Char Methods


16.14. Char Methods

Visual Basic provides a type called a structure that is similar to a class. Although structures and classes are comparable in many ways, structures represent value types. Like classes, structures include methods and properties. Both use the same access modifiers (such as Public, Protected and Private) and access members via the member access operator (.). Classes are created by using the keyword Class, but structures are created using the keyword Structure.

Many of the primitive types that we have used in this book are actually aliases for different structures. For instance, an Integer is defined by structure System.Int32, a Long by System.Int64 and so on. These structures derive from class ValueType, which in turn derives from class Object. In this section, we present structure Char, which is the structure for characters.

Most Char methods are Shared, take at least one character argument and perform either a test or a manipulation on the character. We present several of these methods in the next example. Figure 16.15 demonstrates Shared methods that test characters to determine whether they are of a specific character type and Shared methods that perform case conversions on characters.

Figure 16.15. Char's Shared character-testing and case-conversion methods.

  1  ' Fig. 16.15: SharedCharMethods.vb  2  ' Demonstrates Shared character testing methods  3  ' from Char structure  4  5  Public Class SharedCharMethods  6     ' handle btnAnalyze_Click  7     Private Sub btnAnalyze_Click(ByVal sender As System.Object, _  8        ByVal e As System.EventArgs) Handles btnAnalyze.Clickstructure  9 10        If txtInput.Text.Length > 0 Then 11           ' convert string entered to type char 12           Dim character As Char = Convert.ToChar(txtInput.Text) 13           Dim output As String 14 15           output = "is digit: " & Char.IsDigit(character) & vbCrLf 16           output &= "is letter: " & Char.IsLetter(character) & vbCrLf 17           output &= "is letter or digit: " & _ 18              Char.IsLetterOrDigit(character) & vbCrLf 19           output &= "is lowercase: " & Char.IsLower(character) & vbCrLf 20           output &= "is uppercase: " & Char.IsUpper(character) & vbCrLf 21           output &= "to uppercase: " & Char.ToUpper(character) & vbCrLf 22           output &= "to lowercase: " & Char.ToLower(character) & vbCrLf 23           output &= "is punctuation: " & _ 24              Char.IsPunctuation(character) & vbCrLf 25           output &= "is symbol: " & Char.IsSymbol(character) 26           txtOutput.Text = output 27        End If 28     End Sub ' btnAnalyze_Click 29  End Class ' SharedCharMethods 

(a)

(b)

(c)

(d)

(e)

This Windows application contains a prompt, a TextBox in which the user can input a character, a button that the user can press after entering a character and a second TextBox that displays the output of our analysis. When the user clicks the Analyze Character button, event handler btnAnalyze_Click (lines 728) converts the entered data from a String to a Char, using method Convert.ToChar (line 11).

Line 15 uses Char method IsDigit to determine whether character is a digit. If so, the method returns true; otherwise, it returns False. Line 16 uses Char method IsLetter to determine whether character is a letter. Line 18 uses Char method IsLetterOrDigit to determine whether character is a letter or a digit.

Line 19 uses Char method IsLower to determine whether character is a lowercase letter. Line 20 uses Char method IsUpper to determine whether character is an uppercase letter. Line 21 uses Char method ToUpper to convert character to its uppercase equivalent. The method returns the converted character if the character has an uppercase equivalent; otherwise, the method returns its original argument. Line 22 uses Char method ToLower to convert character to its lowercase equivalent. The method returns the converted character if the character has a lowercase equivalent; otherwise, the method returns its original argument.

Line 24 uses Char method IsPunctuation to determine whether character is a punctuation mark, such as "!", ":" or ")". Line 24 uses Char method IsSymbol to determine whether character is a symbol, such as "+", "=" or "^".

Structure Char also contains other methods not shown in this example. Many of the Shared methods are similarfor instance, IsWhiteSpace determines whether a character is a whitespace character (e.g., newline, tab or space). Char also contains several Public instance methods; many of these, such as methods ToString and Equals, are methods that we have seen before in other classes. This group includes method CompareTo, which is used to compare two character values.




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