Extending the My Namespace

You are not limited to only what the My namespace provides. Just as you can with other namespaces, you can extend this namespace until your heart is content. To show an example of extending the My namespace so that it includes your own functions and properties, in your Console application, create a new module called CompanyExtensions.vb.

The code for the entire module and the associated class is presented here (code file: ProVB_NamespacesCompanyExtensions.vb):

Namespace My
    <HideModuleName()> _
    Module CompanyOperations
        Private _CompanyExtensions As New CompanyExtensions
        Friend Property CompanyExtensions() As CompanyExtensions
            Get
                Return _CompanyExtensions
            End Get
            Set(ByVal value As CompanyExtensions)
                _CompanyExtensions = value
            End Set
        End Property
    End Module
End Namespace
Public Class CompanyExtensions
    Public ReadOnly Property CompanyDateTime() As DateTime
        Get
            Return DateTime.Now()
        End Get
    End Property
End Class

From this example, you can see that the module CompanyOperations is wrapped inside the My namespace. From there, a single property is exposed—CompanyExtensions. The class, CompanyExtensions, is a reference to the class found directly below in the same file. This class, CompanyExtensions, exposes a single ReadOnly Property called CompanyDateTime.

With this in place, build your application, and you are now ready to see the new expanded My namespace in action. Add the following code snippet to Module1.vb. As you type this you'll find that IntelliSense recognizes your custom extension to the My namespace.

System.Windows.Forms.MessageBox.Show(My.CompanyExtensions.CompanyDateTime)

The name of the module CompanyOperations doesn't also appear in the IntelliSense list because the <HideModuleName() > attribute precedes the opening module statement. This attribute signifies that you don't want the module name exposed to the My namespace.

The preceding example shows how to create your own sections within the My namespace, but you can also extend the sections that are already present (for example, Computer, User, etc.). Extending the My namespace is simply a matter of creating a partial class and extending it with the feature sets that you want to appear in the overall My namespace. An example of such an extension is presented in the following code sample (code file: ProVB_NamespacesCompanyExtensions.vb):

Namespace My
    Partial Class MyComputer
        Public ReadOnly Property Hostname() As String
            Get
                Dim iphostentry As System.Net.IPHostEntry = _
                   System.Net.Dns.GetHostEntry(String.Empty)
                Return iphostentry.HostName.ToString()
            End Get
        End Property
    End Class
End Namespace

From this, you can see that this code is simply extending the already present MyComputer class:

Partial Class MyComputer
End Class

This extension exposes a single ReadOnly property called Hostname that returns the local user's hostname. After compiling or utilizing this class in your project, you will find the Hostname property available to you within the My.Computer namespace, as shown in Figure 2.15.

Figure 2.15 IntelliSense for My.Computer

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

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