Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / Visual Basic 10

Extending the My namespace

Rate me:
Please Sign up or sign in to vote.
4.22/5 (6 votes)
8 Aug 2009CPOL7 min read 42.4K   275   32   5
Learn how to extend the My namespace in Visual Basic 2005 / 2008!

Introduction

If you've used the past two versions of Visual Basic (2005 and 2008), then you have most likely heard of, if not used, the My namespace. The My namespace is, in a sentence, a very fast way to access many common .NET functions. What could take many lines of code in raw .NET could be shortened to one line, thanks to My namespace. Each member of My is organized into several sections: My.Application, My.Computer, My.Forms, My.Resources, My.Settings, My.User, and My.WebServices. There is also a downloadable extension off of Microsoft's website, called My.Blogs. All of these members perform specific and useful functions, such as setting the minimum splash screen display time, getting the name of the user's computer, accessing your application's forms, resources, and settings, getting information about the current user, and accessing a Web Service you may have referenced in your program. But, sometimes, you are going to repeat a process over and over again in your program, and it's all going to be raw .NET code. If this was ever to happen, then it would be a good idea to extend the My namespace.

Why should you extend the My namespace? Well, the example I just gave counts as one reason. The My namespace is a shortcut to a large amount of .NET power! Another reason may be to better organize code.

In this article, we are going to extend the My namespace by adding a simple extension that locks the user's computer, perfect for a demonstration like this.

Adding top-level members

First, we're going to learn how to make top-level members, such as My.Computer or My.Blogs. People may make these extensions because the extension that they are developing doesn't quite fit in with the other categories. We are going to create a sample top-level extension called My.SampleExtension. This extension will just return the string “My Namespace Rocks!”; nothing too complicated.

To create a My extension, we need to add a module to the current project. (You need to have a Windows Forms or Console Application project open to continue. This project will also serve as a test case for our extension.) Name the module “MyExtension1” and click OK. You are now presented with an empty module template. You should then see code similar to the following:

VB
Module MyExtension1 

End Module

We now need to wrap the module in the My namespace:

VB
Namespace My

<HideModuleName()> Module MyExtension1 
End Module

End Namespace

Now, since we are going to return a string, we want to tell the extension to return a property that is actually a string.

VB
Namespace My
<HideModuleName()> Module MyExtension1

Friend ReadOnly Property SampleExtension()
Get 
Return ("My Namespace Rocks!") 
End Get

End Property 
End Module
End Namespace

Now go ahead and test your new extension by going into your Windows Forms project and adding a Label. Leave the name Label1, or whatever the label's name is. Double-click the form, and inside the form's loading event handler, type:

VB
Label1.Text = My.Computer.SampleExtension()

Run your application. If everything was done right, you should see a label on your form with the text “My Namespace Rocks!”. Congratulations! You have created your first My extension.

If you wanted to create an extension that exposed more than one property, just like My.Blogs, you could replace the first line with something like this:

VB
Namespace My.WiFi
<HideModuleName()> ...

...and then adding the properties and/or functions inside the module wrapper. This will allow you to type My.WiFi.FunctionName to access the new functions. While adding top-level members to My can be helpful at times, I personally think that the best extensions add functionality to the members that already exist, such as My.Computer.

Adding sublevel members

We are now going to learn how to extend pre-defined members of the My namespace. My.Application and My.Computer are relatively easy to extend. I will show you how to do this in just a minute. My.User is extremely difficult to extend, so we are not going to touch on it in this article. The other members are available for extension, but they are specialized tasks and are on the harder side. We will not be learning about this in this article. We are, however, going to extend My.Computer, and I will show you how to extend My.Application as well.

Add a new module to your project, this time calling it “MyComputerLock”. Delete all of the code that is already included in the module, and create the following wrapper:

VB
Namespace My
Partial Class MyComputer
 
End Class

End Namespace

This tells Visual Basic that the code inside the wrapper needs to be placed inside the My.Computer member of the My namespace. Now, we can start placing the new Subs and properties inside the wrapper. In this case, we want the My namespace to have a lock system function. Type the following code inside the wrapper:

VB
Sub LockSystem()

Shell("rundll32 user32.dll,LockWorkStation", vbNormalFocus)

End Sub

Go ahead and test this extension by adding a button to your form. Double-click the button to access its Click event, and type My.Computer.LockSystem(). Run your program and click the button. It will act as if you had pressed Windows key + L.

That wasn't so bad, was it? To extend the My.Application object, just type Namespace My.Application as the wrapper code when writing your extension.

Deploying My extensions

We are now going to deploy the extension so that it is compatible with Visual Basic 2005 and 2008. It helps if you are using 2005 to start with, but 2008 shouldn't give you too much trouble. Click the File menu, then click Export Template... . Select Item Template from the list, then select the current project you're working on if it's not already selected. Click Next. Select the My Extension module that you wish to export, then go to the next page. Select any of the references that the extension may need, then proceed to the next page. On this final page, specify an icon, name, and description for the extension. Uncheck the box that asks you if you want to automatically import the template into Visual Studio, because we are going to do a little work on the template before we install it. Click Finish, and close Visual Basic. Navigate to your Visual Studio templates folder (in XP, it's My Documents\Visual Studio 200X\My Exported Templates\; in Vista, it's Documents\Visual Studio 200X\My Exported Templates\). You should find the template you just exported. The filename should be the name of the template you gave with a zip extension. Extract the zip file and create a file called info.customdata. This is just a renamed text file that contains the information necessary to install the extension into Visual Basic 2008. Open it in Notepad. Type the following code inside the file:

XML
<VBMyExtensionTemplate 
  ID="Microsoft.VisualBasic.Samples.MyExtensions.MyComputerShutdown" 
  Version="1.0.0.0" />

Save the file, and exit the program. Now, open the .vstemplate file. Inside the <TemplateData> node, type your own child node:

XML
<CustomDataSignature>Microsoft.VisualBasic.MyExtension
</CustomDataSignature>

This tells Visual Basic 2008 that this file is a My extension. Save and close this file. Next, re-package all of the files in this template, including the .customdata file, into a zip file. You can then make a VSI package that will allow you to install this just like a normal template. You can get more information about this in the MSDN documentation installed with Visual Basic. Now, a user can install your template and access it by using the Add New Item... dialog box, or by using the My Extensions tab in the Application Properties page in Visual Basic 2008.

Conclusion

Now that you know how to create your own My namespace extensions, I want to give you a little heads up before concluding.

My extensions can be great. They add extra functionality to an already powerful namespace. However, many a developer has been tempted to use already existing code from the My namespace to build their extensions. Even I have been tempted to do it! However, in most cases, it is not a good idea at all to use code from the existing My namespace to build the extension. It will cause some confusion, and it's really cheating in a way. Plus, if it completely copies something that the My namespace already does, then there's really no need for it, now is there? However, if it takes existing My code and improves and expands on it, then it might be acceptable. This only happens in very rare occasions, though.

Well, I hope you learned a lot from this article. I hope that you get some good ideas for My extensions that will help developers in the present and in the future. I hope to be writing a new article soon that covers extending My.User and other tricky parts of the My namespace.

History

  • Version 1.0 - The first and initial release.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I currently have several years' worth of coding experience, including experience with HTML5, CSS3, JavaScript, Java, Visual Basic.NET, jQuery, PHP, and Python.

Comments and Discussions

 
GeneralMy vote of 4 Pin
Tayab27-Nov-12 16:38
Tayab27-Nov-12 16:38 
GeneralOther resources Pin
Warrick Procter3-Sep-08 9:57
Warrick Procter3-Sep-08 9:57 
Generalcool article Pin
vegeta4ss3-Sep-08 7:06
vegeta4ss3-Sep-08 7:06 
GeneralExtending the My Namespace Pin
Joe Sutphin2-Sep-08 16:29
Joe Sutphin2-Sep-08 16:29 
GeneralRe: Extending the My Namespace Pin
drummerboy05113-Sep-08 3:10
professionaldrummerboy05113-Sep-08 3:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.