1

I have a C# project where the number of classes is heavily increasing and the root project directory is becoming cluttered and unorganized. I want to be able to separate my class.cs files into grouped folders without changing their namespace to perhaps get something like this:

Root directory:
 project.shproj
 Main.cs
 Form1.cs
 Form2.cs
 > UI_Classes folder
   > UI_button.cs
   > UI_header.cs
 > Services folder
   > bgService.cs
   > mainService.cs
 > Util folder
   >calculations.cs
   >abstractMethods.cs
   >extensionMethods.cs

But in doing so I'm getting type or namespace could not be found errors for the classes which I moved, which is annoying because it's so much more organised and cleaner. Is there some way I can do this?

Please do not suggest organizing my classes into namespaces. I am very aware of that and it is not an option.

Edit: Though the CS0246 errors have red underlines, they don't actually stop compiling and running.

Edit2: Not similar to this other question at all. This is referring to a problem with the Visual Studio move feature which was not used, where multiple files have the same name (none of mine do), were being renamed incorrectly (I'm not doing any renaming) in a web project, which mine is not.

Edit3: Has been happening for awhile and has never caused a noticed problem but 3 conflicts occur in the output window when building:

1>  No way to resolve conflict between "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" and "mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e". Choosing "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" arbitrarily.
1>  No way to resolve conflict between "System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" and "System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e". Choosing "System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" arbitrarily.
1>  No way to resolve conflict between "System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" and "System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e". Choosing "System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" arbitrarily.
Moffen
  • 1,817
  • 1
  • 14
  • 34
  • Folders mean nothing. Visual Studio will craft a namespace matching the folder structure (e.g. MyProject.Services, or MyProject.Util), but you're free to change that to anything you like (e.g. MyProject) by editing the `namespace` declaration in the .cs file. – ProgrammingLlama Oct 25 '18 at 00:56
  • Make sure the build action for all CS files is set to "compile". – D Stanley Oct 25 '18 at 00:56
  • This is in an Xamarin Android project in Visual Studio, is there a chance this is relevant? – Moffen Oct 25 '18 at 00:57
  • @John I do not want to edit the namespace declaration - I don't want the namespaces touched at all. – Moffen Oct 25 '18 at 00:59
  • I recommend re-reading my comment very very carefully. By default all classes have namespaces, right? And by default in Visual Studio, if you create a folder `Util`, and then create a new class within that folder, its namespace will be (by default) `MyProject.Util`. As you have said in your question "please do not suggest organizing my classes into namespaces", I am assuming that this is something you don't want, which is why I informed you that you can simply edit the namespace back to be "MyProject". – ProgrammingLlama Oct 25 '18 at 01:01
  • 2
    Can you describe the *"in doing so"* part in more detail? When I drag a file from one folder to another, the namespace is not changed. Is that what you're doing? – Rufus L Oct 25 '18 at 01:05
  • @John thankyou for your input but all files in folders have already had their namespace declarations changed already. Also, files aren't being created in folders as they already exist so Visual Studio isn't creating default namespaces by folder structures anyway. The complete code inthe files is unchanging – Moffen Oct 25 '18 at 01:08
  • Clean and rebuild? – ProgrammingLlama Oct 25 '18 at 01:09
  • Have tried cleans, rebuilds and Visual Studio restarts :( – Moffen Oct 25 '18 at 01:09
  • @RufusL "in doing so" I mean rightClick>Add>newFolder in solution explorer and then dragging existing class files into the new folders. After which I get the CS0246 errors – Moffen Oct 25 '18 at 01:10
  • And you're sure the namespace in the moved classes exactly matches the namespace of your classes which are trying to access them from? – ProgrammingLlama Oct 25 '18 at 01:10
  • Namespaces are exactly the same, only difference is the actual .cs files that can't be referenced are moved one file system folder deeper – Moffen Oct 25 '18 at 01:13
  • 2
    Very strange. I can't reproduce it. Have you tried the suggestions at the bottom of the [CS0246 Documentation](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0246)? Seems more like a problem on visual studio side, possibly something got messed up in the .sln or .suo files (?) – Rufus L Oct 25 '18 at 01:14

1 Answers1

3

they don't actually stop compiling and running

What you have is a bug or stale cache somewhere, and i have seen it more times than i care to admit.

1. Try clean up your solution

  • Clean
  • Delete bin and obj directories
  • Restart

2. .Net Standard libs mismatch

.net standard libs can also be a problem in Xamarin, try re adding the references to all your .Net Standard libraries

3. Resharper problem

Resharper gets confused with its cache

Tool -> Options -> Resharper 
  1. Suspend
  2. Resume

4. Visual studio problem

If that doesn't work it could be a visual studio thing

Try deleting the Solution User Options (.Suo) file located in your solution .vs directory somewhere

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • I do get 3 conflicts in the build log output when building, I will post them as a question edit and then try your suggested fixes – Moffen Oct 25 '18 at 01:20
  • Not sure what was happening but you were exactly right, deleting the bin, obj, .suo files and doing a full restart and rebuild has the project working again. Can't express enough thanks – Moffen Oct 25 '18 at 01:41