Extend the My Namespace

The My objects aren't defined in a single place. Some come from classes defined in the Microsoft.VisualBasic.MyServices namespace, while others are generated dynamically as you add forms, web services, configuration settings, and embedded resources to your project. However, as a developer you can participate in the My namespace and extend it with your own ingredients (e.g., useful calculations and tasks that are specific to your application).

Note

Do you use the My objects so much you'd like to customize them yourself? VB 2005 lets you plug in your own classes.

How do I do that?

To plug a new class into the My object hierarchy, simply use a Namespace block with the name My. For example, you could add this code to create a new BusinessFunctions class that contains a company-specific function for generating custom identifiers (by joining the customer name to a new GUID):

Namespace My
    
    Public Class BusinessFunctions
        Public Shared Function GenerateNewCustomerID( _
          ByVal name As String) As String
            Return name & "_" & Guid.NewGuid.ToString( )
        End Function
    End Class
    
End Namespace

Once you've created the BusinessFunctions object in the right place, you can make use of it in your application just like any other My object. For example, to display a new customer ID:

Console.WriteLine(My.BusinessFunctions.GenerateNewCustomerID("matthew"))

Note that the My classes you add need to use shared methods and properties. That's because the My object won't be instantiated automatically. ...

Get Visual Basic 2005: A Developer's Notebook 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.