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.
1: <PropertyGroup>
2: <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
3: <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
4: <ProductVersion>9.0.30729</ProductVersion>
5: <SchemaVersion>2.0</SchemaVersion>
6: <ProjectGuid>{99D23E3F-D6D5-467F-AB1D-A594E40F4378}</ProjectGuid>
7: <OutputType>WinExe</OutputType>
8: <StartupObject>RemoveMyNamespace.Form1</StartupObject>
9: <RootNamespace>RemoveMyNamespace</RootNamespace>
10: <AssemblyName>RemoveMyNamespace</AssemblyName>
11: <FileAlignment>512</FileAlignment>
12: <MyType>Empty</MyType>
13: <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
14: <OptionExplicit>On</OptionExplicit>
15: <OptionCompare>Binary</OptionCompare>
16: <OptionStrict>Off</OptionStrict>
17: <OptionInfer>On</OptionInfer>
18: </PropertyGroup>
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.