14.4. Your Turn

While the My namespace is already loaded with numerous productivity shortcuts, it has been put together with the average Visual Basic developer in mind. There are always going to be cases where you go looking for a shortcut that just isn't there. In these cases it's possible to extend the namespace in a couple of different ways.

14.4.1. Methods and Properties

The simplest way to extend the My namespace is to add your own methods or properties. These can be stand-alone, or they can belong to one of the existing My namespace classes. For example, the following function, which extracts name-value pairs from a string into a dictionary, is a stand-alone function and will appear at the top level of the My namespace.

Namespace My
    <HideModuleName()> _
    Module StringHelpers
        Friend Function ParseString(ByVal stringToParse As String, _
                                     ByVal pairSeparator As Char, _
                                     ByVal valueSeparator As Char) _
                                                  As Dictionary(Of String, String)
            Dim dict As New Dictionary(Of String, String)
            Dim nameValues = From pair In stringToParse.Split(pairSeparator), _
                                   values In pair.Split(valueSeparator) _
                             Select New With {.Name = values(0), _
                                              .Value = values(1)}
            For Each nv In nameValues
                dict.Item(nv.Name) = nv.Value
            Next
            Return dict
        End Function

   End Module
End Namespace

Figure 14-3 illustrates that the StringHelpers module is completely hidden when you're accessing this function.

Figure 14.3. Figure 14-3

As both My.Application and My.Computer return an instance of a generated partial class, you ...

Get Professional Visual Studio® 2008 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.