Removing My Namespace from VB.Net
Recently a question arose on Stackoverflow that asked if you could remove the My namespace from vb.net.
So, before I get going with this article, I want to state that the My Namespace does have a few uses, it provides instant access to resources, settings, and quick environment settings.
I also want to state that THIS IS NOT A REQUIRED FEATURE IN VB.NET. Did I stress that yet? It is however a default feature in VB.Net.
Anyway, lets get on with it.
First things first, backup your project directory, I don’t want to be responsible for you deleting any of your settings or resources because you want to remove something you are using.
Now that you did that (right?) lets move to the solution explorer and click on the option to “Show all files”.
Expand the “My Project” Node and select the “Application.myapp”, “Resources.resx”, and “Settings.settings” nodes. When I say nodes, that means items below “My Project”, this is a treeview.
Now, hit delete. This will remove any of the “My” code that has automatically already been added to your project. Go ahead and click on the “Show all files” button again to get back to a clean view.
Next, double click on “My Project” and navigate to the Compile tab, and click on the “Advanced Compile Options”. This dialog has all kinds of fun stuff in it, but we are only worried about one particular setting. Go ahead and click on “Enable Optimizations”.
Click “OK” and we are about 50% done.
Now, in the solution explorer, right click on your project and select “Unload Project”.
This will unload your project from the IDE, but retain the reference to it, another great thing about it is it allows us to edit the .vbproj file directly instead of through the UI, which is what is required for us to do the next step.
Look for the <MyType> xml tag, we need to set this to “Empty”, not Empty, but with the value of “Empty”. You may also need to change the <StartupObject> tag to reflect your main form if it is currently set to “My.Application”.
Below is the XML from the first PropertyGroup after modifying it.
2: <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
3: <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
I bolded the change.
Save the file and close it, now right click on the project in the solution explorer and reload the project.
Compile it, and you can now view it in Reflector to see that the “My” namespace is completely removed.
We have now successfully remove the “My” namespace.
The keyword still exists, but it is now removed completely from your project.