![]() |
Languages »
VB.NET »
General
Beginner
License: The Code Project Open License (CPOL)
Extending the My NamespaceBy drummerboy0511Learn how to extend the My Namespace in Visual Basic 2005 / 2008! |
VB (VB 7.x, VB 8.0, VB 9.0, VB 6), Windows (WinXP, Vista), Visual Studio (VS2005, VS2008), Dev
|
||||||||
|
Advanced Search |
|
|
|
||||||||||||||||
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 the 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 access one of the Web services 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 other code. We are going to extend the My Namespace by adding an extension that locks the user's computer.
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:
Module MyExtension1
End Module
We now need to wrap the module in the
My Namespace:
Namespace
My
<HideModuleName()>
Module MyExtension1
End ModuleEnd 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.
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 add 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:
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:
Namespace
My.WiFi
<HideModuleName()>
...
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:
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:
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.
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. We want to do a little work on the template before we install
it. Close Visual Basic. Navigate to your Visual Studio templates
folder (Which is My Documents (Documents)\Visual Studio (version#)\My
Exported Templates. You should find the template you just exported.
The filename should be template.zip. 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:
<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:
<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. Then, you can package it into a Visual Studio Installer file. 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.
Now that you know how to create your own My Namespace extensions, I wanted 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.
Version 1.0 - The first and initial release.
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 31 Aug 2008 Editor: |
Copyright 2008 by drummerboy0511 Everything else Copyright © CodeProject, 1999-2009 Web17 | Advertise on the Code Project |