EXTENSION METHODS

Extension methods allow you to add new methods to an existing class without rewriting it or deriving a new class from it. To make an extension method, place the method in a code module and decorate its declaration with the Extension attribute. The first parameter determines the class that the method extends. The method can use that parameter to learn about the item for which the method was called. The other parameters are passed into the method so it can use them to perform its chores.

For example, the following code adds a MatchesRegexp subroutine to the String class:

' Return True if a String matches a regular expression.
<Extension()>
Public Function MatchesRegexp(the_string As String,
 ByVal regular_expression As String) As Boolean
    Dim reg_exp As New Regex(regular_expression)
    Return reg_exp.IsMatch(the_string)
End function

The Extension attribute tells Visual Basic that this is an extension method. The method’s first parameter is a String so this method extends the String class. The second parameter is a regular expression. The method returns True if the String matches the regular expression.

The following code shows how a program might use this method to decide whether the string stored in variable phone_number looks like a valid 7-digit United States phone number:

if Not phone_number.MatchesRegexp("^[2-9]d{2}-d{4}$") Then
    MessageBox.Show("Not a valid phone number")
End if

Example program ValidatePhone, which is available for download on the book’s website, demonstrates the MatchesRegexp extension method. It also uses the MatchesRegexp method to define the following three additional extension methods that determine whether a string looks like a valid 7- or 10-digit United States phone number. These methods simply call the MatchesRegexp method, passing it appropriate regular expressions.

' Return True if a String looks like a 7-digit US phone number.
<Extension()>
Public Function IsValidPhoneNumber7digit(the_string As String) As Boolean
    Return the_string.MatchesRegexp("^[2-9]d{2}-d{4}$")
End Function
 
' Return True if a String looks like a 10-digit US phone number.
<Extension()>
Public Function IsValidPhoneNumber10digit(the_string As String) As Boolean
    Return the_string.MatchesRegexp("^([2-9]d{2}-){2}d{4}$")
End Function
 
' Return True if a String looks like a 7- or 10-digit US phone number.
<Extension()>
Public Function IsValidPhoneNumberUS(the_string As String) As Boolean
    Return IsValidPhoneNumber7digit(the_string) OrElse
           IsValidPhoneNumber10digit(the_string)
End function

If you build a class and later need to change its features, it’s usually easiest to modify its code directly. That will cause less confusion than extension methods, which may lie in some obscure module that seems unrelated to the original class. If you need to add methods to existing classes that you cannot modify directly, such as String and other classes defined by Visual Basic and the .NET Framework, extension methods can be extremely useful.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.70.248