MSDN Magazine - April 2008 - (Page 27) ing My.Computer.Network, and customizing the way My.Settings stores application configuration information. What about objects such as My.WebServices and My.Forms? The classes of these objects are nested types defined in Modules, which means they cannot be used with the Partial Class feature, and they are NotInheritable, which means they cannot be extended like My.User. There is no easy way to add members to them. In this case, you must find a better way for your extension to fit into the My namespace. Coding the Extension Now that I’ve explained extensions to the My namespace, let’s see how you can easily implement and share these extensions in our projects. I just showed how My is a namespace like any other namespace, and you can add top-level properties to it by simply adding a module and specifying a namespace of My. Let’s now create a set of validation functions that can check if a string matches the format of U.S. and Canadian postal codes, phone numbers, email addresses, and a few other important formats. Let’s begin by starting Visual Studio® and creating a new console application. Name the project MyValidation. Next, add a new Module to the solution called MyValidation.vb and replace the code in the MyValidation.vb file with the code shown in Figure 5 (which has been excerpted here for space). Figure 5 Validation Code Excerpt Imports Imports Imports Imports System.Text.RegularExpressions System System.Linq Microsoft.VisualBasic Be careful when creating a My Extension template that your code can work in any project configuration. You need to pay particular attention to the possible values of Option Explicit, Option Strict, Option Compare, and (new in Visual Basic 2008) Option Infer. You can either write code that will work in the least-flexible configuration (Option Explicit On, Option Strict On, Option It’s a hassle to re-export your Infer Off, and Option Comextension when you find a pare unspecified), or specify bug, so make the extension each option explicitly at the top of each code file in your as functional as possible extension, which is the recand make sure it’s properly ommended approach. tested before you export it Also, you need to be carefor the first time. ful to account for different project imports to prevent conflict with symbols defined in the project. You do this by qualifying any symbol not defined in your template with the Global or the My keyword. For instance, instead of Text.Encoding, use Global.System.Text.Encoding. If you only use Text.Encoding, your template will not work in Windows® Forms Applications, which imports two namespaces that contain a namespace called Text. If you omit Global and only use System.Text.Encoding, then your template may not work in a project that defines a namespace called System. Namespace My.Validation _ Public Module MyValidationMod Public Function IsEmpty(ByVal value As Object) As Boolean If (value Is Nothing) OrElse _ (value Is System.DBNull.Value) OrElse _ (value.ToString = String.Empty) OrElse _ (TypeOf value Is Date AndAlso CDate(value) = Date.MinValue) _ Then Return True Else Return False End If End Function Public Function IsAlphaNumeric(ByVal value As String, _ Optional ByVal emptyOK As Boolean = False, Optional ByVal _ whitespaceOK As Boolean = False) As Boolean If IsEmpty(value) Then Return emptyOK Dim expr As String If whitespaceOK Then expr = “^[a-zA-Z0-9\s]+$” Else expr = “^[a-zA-Z0-9]+$” End If Return Regex.IsMatch(value, expr) End Function Public Function IsCanadianProvince(ByVal st As String) As Boolean Dim allProv = “|AB|BC|MB|NB|NL|NT|NS|NU|ON|PE|QC|SK|YT” Return st.Length = 2 AndAlso allProv.IndexOf(“|” & st) -1 End Function Public Function IsUSAState(ByRef st As String) As Boolean Dim allStates = “|AL|AK|AZ|AR|CA|CO|CT|DE|FL|GA|HI|ID|IL|IN|IA|” & _ “KS|KY|LA|ME|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|OH|” & _ “OK|OR|PA|RI|SC|SD|TN|TX|UT|VT|VA|WA|WV|WI|WY” Return st.Length = 2 AndAlso allStates.IndexOf(“|” & st) -1 End Function Public Function IsUSAPostalCode(ByVal zip As String) As Boolean If String.IsNullOrEmpty(zip) Then Return False Return Regex.IsMatch(zip, “^\d{5}(-\d{4})?$”) End Function Public Function IsCanadianPostalCode(ByVal zip As String) As Boolean If String.IsNullOrEmpty(zip) Then Return False Return Regex.IsMatch(zip, “^[A-Z]\d[A-Z] \d[A-Z]\d$”) End Function Public Function IsNorthAmericanPhone(ByRef phone As String) As Boolean If String.IsNullOrEmpty(phone) Then Return False Dim expr As String = “^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}” & _ “(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4}$” Return Regex.IsMatch(phone, expr) End Function Public Function IsEmail(ByRef email As String) As Boolean If String.IsNullOrEmpty(email) Then Return False Dim localPartCharset = “[0-9a-zA-Z!#$%*/?|\^{}`~&’+\-=_]” Dim domainPartCharset = “[0-9a-zA-Z\-]” Dim expr = String.Format(“^{0}+(\.{0}+)*@{1}+(\.{1}+)*”, _ localPartCharset, domainPartCharset) Return Regex.IsMatch(email, expr) End Function End Module End Namespace Basic Instincts april2008 27
Table of Contents Feed for the Digital Edition of MSDN Magazine - April 2008 MSDN Magazine - April 2008 Contents Toolbox CLR Inside Out Basic Instincts Cutting Edge Foundations Test Run Service Station Windows with C++ Going Places { End Bracket } MSDN Magazine - April 2008 MSDN Magazine - April 2008 - (Page Intro) MSDN Magazine - April 2008 - Contents (Page Cover1) MSDN Magazine - April 2008 - Contents (Page Cover2) MSDN Magazine - April 2008 - Contents (Page 1) MSDN Magazine - April 2008 - Contents (Page 2) MSDN Magazine - April 2008 - Contents (Page 3) MSDN Magazine - April 2008 - Contents (Page 4) MSDN Magazine - April 2008 - Contents (Page 5) MSDN Magazine - April 2008 - Contents (Page 6) MSDN Magazine - April 2008 - Contents (Page 7) MSDN Magazine - April 2008 - Contents (Page 8) MSDN Magazine - April 2008 - Contents (Page 9) MSDN Magazine - April 2008 - Contents (Page 10) MSDN Magazine - April 2008 - Toolbox (Page 11) MSDN Magazine - April 2008 - Toolbox (Page 12) MSDN Magazine - April 2008 - Toolbox (Page 13) MSDN Magazine - April 2008 - Toolbox (Page 14) MSDN Magazine - April 2008 - Toolbox (Page 15) MSDN Magazine - April 2008 - Toolbox (Page 16) MSDN Magazine - April 2008 - CLR Inside Out (Page 17) MSDN Magazine - April 2008 - CLR Inside Out (Page 18) MSDN Magazine - April 2008 - CLR Inside Out (Page 19) MSDN Magazine - April 2008 - CLR Inside Out (Page 20) MSDN Magazine - April 2008 - CLR Inside Out (Page 21) MSDN Magazine - April 2008 - CLR Inside Out (Page 22) MSDN Magazine - April 2008 - CLR Inside Out (Page 23) MSDN Magazine - April 2008 - CLR Inside Out (Page 24) MSDN Magazine - April 2008 - Basic Instincts (Page 25) MSDN Magazine - April 2008 - Basic Instincts (Page 26) MSDN Magazine - April 2008 - Basic Instincts (Page 27) MSDN Magazine - April 2008 - Basic Instincts (Page 28) MSDN Magazine - April 2008 - Basic Instincts (Page 29) MSDN Magazine - April 2008 - Basic Instincts (Page 30) MSDN Magazine - April 2008 - Basic Instincts (Page 31) MSDN Magazine - April 2008 - Basic Instincts (Page 32) MSDN Magazine - April 2008 - Basic Instincts (Page 33) MSDN Magazine - April 2008 - Basic Instincts (Page 34) MSDN Magazine - April 2008 - Cutting Edge (Page 35) MSDN Magazine - April 2008 - Cutting Edge (Page 36) MSDN Magazine - April 2008 - Cutting Edge (Page 37) MSDN Magazine - April 2008 - Cutting Edge (Page 38) MSDN Magazine - April 2008 - Cutting Edge (Page 39) MSDN Magazine - April 2008 - Cutting Edge (Page 40) MSDN Magazine - April 2008 - Cutting Edge (Page 41) MSDN Magazine - April 2008 - Cutting Edge (Page 42) MSDN Magazine - April 2008 - Cutting Edge (Page 43) MSDN Magazine - April 2008 - Cutting Edge (Page 44) MSDN Magazine - April 2008 - Cutting Edge (Page 45) MSDN Magazine - April 2008 - Cutting Edge (Page 46) MSDN Magazine - April 2008 - Foundations (Page 47) MSDN Magazine - April 2008 - Foundations (Page 48) MSDN Magazine - April 2008 - Foundations (Page 49) MSDN Magazine - April 2008 - Foundations (Page 50) MSDN Magazine - April 2008 - Foundations (Page 51) MSDN Magazine - April 2008 - Foundations (Page 52) MSDN Magazine - April 2008 - Foundations (Page 53) MSDN Magazine - April 2008 - Foundations (Page 54) MSDN Magazine - April 2008 - Foundations (Page 55) MSDN Magazine - April 2008 - Foundations (Page 56) MSDN Magazine - April 2008 - Foundations (Page 57) MSDN Magazine - April 2008 - Foundations (Page 58) MSDN Magazine - April 2008 - Foundations (Page 59) MSDN Magazine - April 2008 - Foundations (Page 60) MSDN Magazine - April 2008 - Foundations (Page 61) MSDN Magazine - April 2008 - Foundations (Page 62) MSDN Magazine - April 2008 - Foundations (Page 63) MSDN Magazine - April 2008 - Foundations (Page 64) MSDN Magazine - April 2008 - Foundations (Page 65) MSDN Magazine - April 2008 - Foundations (Page 66) MSDN Magazine - April 2008 - Foundations (Page 67) MSDN Magazine - April 2008 - Foundations (Page 68) MSDN Magazine - April 2008 - Foundations (Page 69) MSDN Magazine - April 2008 - Foundations (Page 70) MSDN Magazine - April 2008 - Foundations (Page 71) MSDN Magazine - April 2008 - Foundations (Page 72) MSDN Magazine - April 2008 - Foundations (Page 73) MSDN Magazine - April 2008 - Foundations (Page 74) MSDN Magazine - April 2008 - Foundations (Page 75) MSDN Magazine - April 2008 - Foundations (Page 76) MSDN Magazine - April 2008 - Foundations (Page 77) MSDN Magazine - April 2008 - Foundations (Page 78) MSDN Magazine - April 2008 - Foundations (Page 79) MSDN Magazine - April 2008 - Foundations (Page 80) MSDN Magazine - April 2008 - Foundations (Page 81) MSDN Magazine - April 2008 - Foundations (Page 82) MSDN Magazine - April 2008 - Foundations (Page 83) MSDN Magazine - April 2008 - Foundations (Page 84) MSDN Magazine - April 2008 - Foundations (Page 85) MSDN Magazine - April 2008 - Foundations (Page 86) MSDN Magazine - April 2008 - Foundations (Page 87) MSDN Magazine - April 2008 - Foundations (Page 88) MSDN Magazine - April 2008 - Foundations (Page 89) MSDN Magazine - April 2008 - Foundations (Page 90) MSDN Magazine - April 2008 - Foundations (Page 91) MSDN Magazine - April 2008 - Foundations (Page 92) MSDN Magazine - April 2008 - Foundations (Page 93) MSDN Magazine - April 2008 - Foundations (Page 94) MSDN Magazine - April 2008 - Foundations (Page 95) MSDN Magazine - April 2008 - Foundations (Page 96) MSDN Magazine - April 2008 - Foundations (Page 97) MSDN Magazine - April 2008 - Foundations (Page 98) MSDN Magazine - April 2008 - Test Run (Page 99) MSDN Magazine - April 2008 - Test Run (Page 100) MSDN Magazine - April 2008 - Test Run (Page 101) MSDN Magazine - April 2008 - Test Run (Page 102) MSDN Magazine - April 2008 - Test Run (Page 103) MSDN Magazine - April 2008 - Test Run (Page 104) MSDN Magazine - April 2008 - Test Run (Page 105) MSDN Magazine - April 2008 - Test Run (Page 106) MSDN Magazine - April 2008 - Service Station (Page 107) MSDN Magazine - April 2008 - Service Station (Page 108) MSDN Magazine - April 2008 - Service Station (Page 109) MSDN Magazine - April 2008 - Service Station (Page 110) MSDN Magazine - April 2008 - Service Station (Page 111) MSDN Magazine - April 2008 - Service Station (Page 112) MSDN Magazine - April 2008 - Service Station (Page 113) MSDN Magazine - April 2008 - Service Station (Page 114) MSDN Magazine - April 2008 - Windows with C++ (Page 115) MSDN Magazine - April 2008 - Windows with C++ (Page 116) MSDN Magazine - April 2008 - Windows with C++ (Page 117) MSDN Magazine - April 2008 - Windows with C++ (Page 118) MSDN Magazine - April 2008 - Windows with C++ (Page 119) MSDN Magazine - April 2008 - Windows with C++ (Page 120) MSDN Magazine - April 2008 - Windows with C++ (Page 121) MSDN Magazine - April 2008 - Windows with C++ (Page 122) MSDN Magazine - April 2008 - Going Places (Page 123) MSDN Magazine - April 2008 - Going Places (Page 124) MSDN Magazine - April 2008 - Going Places (Page 125) MSDN Magazine - April 2008 - Going Places (Page 126) MSDN Magazine - April 2008 - Going Places (Page 127) MSDN Magazine - April 2008 - { End Bracket } (Page 128) MSDN Magazine - April 2008 - { End Bracket } (Page Cover3) MSDN Magazine - April 2008 - { End Bracket } (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.