Extending the My Namespace

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

DevPro Staff

July 13, 2004

6 Min Read
ITPro Today logo in a gray background | ITPro Today

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

 Summary: Thisarticle talks about extending the Mynamespace and adding your custom classes into the My hierarchy

 Visual Basic 2005 ships with a new productivityfeature called the My namespace. Myis a shortcut to access some of the most commonly usedfeatures/functions of .NET in a categorized and easy manner. For example to findif your machine has a Mouse connected, the framework provides you with afunction, 

System.Windows.Forms.SystemInformation.MousePresent()

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

My.Computer.Mouse.Exists()

 That is exactly what the Mynamespace 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 commonreason can be you are writing a library that works with a new device, say abarcode reader or a digital camera. I would add a lot of value if you can makethis a part of the My namespace sothat developers can easily work with your components. Say something like, 

My.Computer.Camera.GetPictures()

 willmake it easier for the developer to use your library.

 Using Myeases finding your component, as it is easier for a programmer to guess where hecan find a particular feature or function. So this increases the discoverabilityof your component.

 So how do you get your classes to get into beinglisted 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 willbecome 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

    PublicClass Camera

       PublicFunction GetPictures(AsBitmap)

 

       End Function

    EndClass

EndNamespace

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

 

But you will not be able to use this approach to add your classunder the My.Computer or other partsof the My namespace hierarchy. This isbecause these other components of the Mynamespace are currently implemented as individual classes rather than asnamespaces.

 So you cannot do something like

Namespace My.Computer

    PublicClass Camera

       PublicFunction GetPictures(AsBitmap)

 

       End Function

    EndClass

EndNamespace

 

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

So the disadvantage of using this method of extending is
1.     Your class has to be created as a part of the namespace Myand so if you create a component that will also be used by C# programmers, theywill 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.Computeris really a class. So to extend that, you can use a new feature available inWhidbey called Partial Classes. This allows you to have a classimplementation spread across multiple physical files. Thus you can really extendthe My.Computer class by addingproperties and methods to it.

The only trick is that you need to know the name of theclass that is mapped to My.Computernamespace. In reality the class is named MyComputerand 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 (assumingyou have implemented all your Camera related functionalities in a class called Devices.Camera):

 

Namespace My

    PartialClass MyComputer

       PublicReadOnly PropertyCamera() AsDevices.Camera

           Get

 

           End Get

       End Property

    EndClass

EndNamespace

 

This adds a property called Camera to the My.Computernamespace 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 thefirst one, but then it provides you with more flexibility of adding your classin any place in the My Hierarchy. The only difficulty is finding the name of theactual class. A little looking around with ILDasm should help you get thenecessary information.

 

Some of the commonly used Classes are as below:

Namespace

Class Name

Computer

Microsoft.VisualBasic.MyServices .MyComputer

Application

.My.MyApplication

Forms

.My.MyForms

 

Handling of My Classes

 

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

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

When you extend the existing partial classes to add yourown extensions, you automatically get access to these special services of the VBcompiler. Currently there are no documented way to extended this feature toclasses you create in the MyNamespace.

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

References:

http://www.dotnetindia.com/2004/05/extending_my_na.html

http://www.dotnetindia.com/2004/05/more_on_extendi.html

http://msdn.microsoft.com/vbasic/default.aspx?pull=/msdnmag/issues/04/05/visualbasic2005/default.aspx

Read more about:

Microsoft
Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like