Visual Studio namespace convention - c#

I want that all of the files in an assembly will have the assembly name as namespace.
For example, if I create a new class in MyAssembly.SomeFolder - I want that the class defualt namespace will be "MyAssembly" and not "MyAssembly.SomeFolder.
Is there a way to configure this on VS?

You can change the Visual Studio class template for C# to bypass this.
You may want to look into the Visual Studio templates here: http://msdn.microsoft.com/en-us/library/ms247121.aspx
There are no real settings for this, unless you have ReSharper installed. Then you can set Namespace Provider to false.

Related

Visual studio: Startup Object in Application is not showing latest added class name which contains Main() method

I create/delete classes in Visual studio but when i choose: Solution explorer -> app -> property-> Application-> Startup object i don't see the changes.
Beacause information that you offered is limited. I only speculate reason, it's possible that the classes were not added to the correct project or namespace.
Here are some steps you can take to troubleshoot this issue:
Check that the classes were added to the correct project: Make sure that the classes you created are in the project that you are trying to set the startup object for. If the classes are in a different project, they will not be available as a startup object in the project properties.
Check the namespace: Make sure that the namespace of the classes you created matches the namespace of the project. If the namespace is different, the classes will not be available as a startup object in the project properties.
Rebuild the solution: After you have added new classes to your project, you need to rebuild the solution before they are available for use. Right-click on the solution in the Solution Explorer and select "Rebuild Solution" to ensure that all the classes are compiled and available.
Restart Visual Studio: If none of the above steps resolve the issue, try restarting Visual Studio. Sometimes Visual Studio can get stuck and needs to be restarted to refresh the solution explorer.
If none of these steps help, you can try creating a new project and adding your classes to the new project. This will ensure that the classes are added to the correct project and namespace from the beginning.
Lastly, I use Console to testing. If I want to show new class on Startup object, I have to add static void Main(string[] args){} in class and debug this project successfully. If above steps didn't help you, please offer more information.

Xamarin Studio how to write namespaces

In C# is normal to write namespace
namespace ProjectName.Folder so if project is Test and then folder name inside project is Utils then namespace is
namespace Test.Utils
In Xamarin projects in Github namespace is always the same so just
namespace Test
so what is now better to use, always the same namespace or like in Visual studio?
This is mostly personal preference. Xamarin Studio by default uses a single namespace per project, which is probably why most examples you see are written that way. There is an option you can change to make it behave more like Visual Studio
Preferences --> Source Code --> .NET Naming Policies --> "Associate
namespaces with directory names"

Visual studio does not exist in the namespace, but reference is added

I am building a custom code coverage app using the Microsoft.VisualStudio.Coverage.Analysis class.
I have added the reference to the dll to my solution already and when I start typing the using statement at the top of my class Visual Studio is predicting the correct names, e.g. after typing "Microsoft." "VisualStudio" will be displayed in the suggestion box, the same thing happens with the next two levels, Coverage and Analysis.
But the second I try to build the project I get the following error:
the type or namespace name 'visual studio' does not exist in the namespace 'microsoft'
I've no idea why this is happening so any suggestions would be great
I should have checked the warnings in visual studio, they had the real error details, the project had to be built against .net 4.5 and not 4.0.
Once this was changed the project built correctly.

Adding the solution name as base of namespace for all projects

So I have this solution in visual studio 2013. Currently the solution tree looks like this:
MySolution
>MyProjectA
>MyProjectB
>MyProjectC
The default is that each class in the project folder has the namespace same as the project folder name.
But I would like that each class in each project folder have a namespace that starts with the solution name. For example if there is a class named MyClass in MyProjectA I want it's namespace to be MySolution.MyProjectA
Is there a way to automatically to this in Visual Studio? I could go and change all the namespaces my self...but I rather would love to see if its possible to do this automatiaclly.
In each project's settings, there is a Default Namespace that is used whenever you add new files to a project. The default value is the project name, but you can customize it. You can change that and then any new files added will use the desired namespace.
For any existing files, a Find and Replace can provide a relatively quick cleanup (find: namespace MyProjectA -> replace with: namespace MySolution.MyProjectA, only search within current project).
Download ReSharper, it has a lot of such features. Install it, after that you just have to rename each project as MySolution.ProjectA,B,C and then right click on the project and select option refactor and then click adjust namespaces. This will do the trick, only thing I see here is that you will need to rename your projects.

Visual Studio adding class library - prefix with Solution name

This is going to probably going to sound really silly but I am not sure what happened.
My hard drive crashed and I had to re-install software including Visual Studio 2012.
OK. Installed. Create solution. Add a class library.
Created solution 'MySolution'
Add new Class library 'Common'
Previously:
The code behind namespace: MySolution.Common
Now:
The code behind namespace: Common
I don't recall doing any specific settings but I would really want the namespace to be MySolution.Common again.
Any advice on how I would set the namespace is greatly appreciated.
thanks!
You probably want to set the "default namespace" of your library project (Project properties > Application tab).

Categories