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 ...
Get Professional Visual Basic 2012 and .NET 4.5 Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.