Extending the My Namespace

(This article talks about features available in the Beta 1 of Visual Studio 2005)

 Summary: This article talks about extending the My namespace and adding your custom classes into the My hierarchy

 Visual Basic 2005 ships with a new productivity feature called the My namespace. My is a shortcut to access some of the most commonly used features/functions of .NET in a categorized and easy manner. For example to find if your machine has a Mouse connected, the framework provides you with a function, 


 But would it not be easier and more intuitive if I can do something like 


 That is exactly what the My namespace enables. More information on this is available in the MSDN article http://msdn.microsoft.com/vbasic/default.aspx?pull=/msdnmag/issues/04/05/visualbasic2005/default.aspx


Why Extend the My Namespace


If it has it all, why change it? Well the most common reason can be you are writing a library that works with a new device, say a barcode reader or a digital camera. I would add a lot of value if you can make this a part of the My namespace so that developers can easily work with your components. Say something like, 


 will make it easier for the developer to use your library.

 Using My eases finding your component, as it is easier for a programmer to guess where he can find a particular feature or function. So this increases the discoverability of your component.

 So how do you get your classes to get into being listed as a part of the My Namespace. There are two different ways you can get your classes to become a part of My. Currently it is not an easy and clean way of doing it, but hopefully it will become easier to do in the release version.


Adding a Class to My

 The simplest way of extending My is to create your class under a namespace My

Namespace My

    Public Class Camera

        Public Function GetPictures(As Bitmap)


        End Function

    End Class

End Namespace

 This creates a class Camera under the My namespace. So when you refer to this assembly in your project you will get: 


But you will not be able to use this approach to add your class under the My.Computer or other parts of the My namespace hierarchy. This is because these other components of the My namespace are currently implemented as individual classes rather than as namespaces.

 So you cannot do something like

Namespace My.Computer

    Public Class Camera

        Public Function GetPictures(As Bitmap)


        End Function

    End Class

End Namespace


This will result in your class being available in the My.Computer, but you will lose all the existing functions in the My.Computer namespace.

So the disadvantage of using this method of extending is
1.      Your class has to be created as a part of the namespace My and so if you create a component that will also be used by C# programmers, they will be using a namespace that does not make sense to them
2.      And you can only put your class in the root of the My hierarchy and not under one of the sub components.


Adding a Class to My.Computer

As mentioned previously, My.Computer is really a class. So to extend that, you can use a new feature available in Whidbey called Partial Classes. This allows you to have a class implementation spread across multiple physical files. Thus you can really extend the My.Computer class by adding properties and methods to it.

The only trick is that you need to know the name of the class that is mapped to My.Computer namespace. In reality the class is named MyComputer and is available in the DLL, Microsoft.VisualBasic.DLL.

So to add our Camera class to the My.Computer namespace we can extend this class and create (assuming you have implemented all your Camera related functionalities in a class called Devices.Camera):


Namespace My

    Partial Class MyComputer

        Public ReadOnly Property Camera() As Devices.Camera



            End Get

        End Property

    End Class

End Namespace


This adds a property called Camera to the My.Computer namespace and this property returns and instance of the Camera object.

So now you can access your Camera class from My.Computer.

This method of course is a little more complex than the first one, but then it provides you with more flexibility of adding your class in any place in the My Hierarchy. The only difficulty is finding the name of the actual class. A little looking around with ILDasm should help you get the necessary information.


Some of the commonly used Classes are as below:


Class Name


Microsoft.VisualBasic.MyServices .MyComputer






Handling of My Classes


The VB compiler does some internal magic to create the My Classes. The My Classes are created via a factory method internally so that it is context sensitive. In EXEs and DLLs the objects are created per thread. This magic happens because the VB compiler emits an extra set of classes when it compiles your project.

Each project get a set of classes called MyApplication, MyComputer and MyProject under the namespace .My. All calls to My namespace are now redirected to MyProject equivalents. This allows the VB compiler to make sure that it returns only a single instance per thread.

When you extend the existing partial classes to add your own extensions, you automatically get access to these special services of the VB compiler. Currently there are no documented way to extended this feature to classes you create in the My Namespace.

Another gotcha to be kept in mind is when using Shared variables inside of your My extensions. Do keep in mind that your class may have different instances in different threads. So access to Shared instance variables will have to have code to handle thread synchronization issues.





Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.