O'Reilly logo

Professional Visual Basic 2012 and .NET 4.5 Programming by Todd Herman, Gastón Hillar, David McCarter, Rob Windsor, Billy Hollis, Bill Sheldon

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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_Namespaces\CompanyExtensions.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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required