Namespaces and folder relation - c#

Ok, I guess do namespaces have to be the same as the nested folders that their in? I keep getting errors saying that the namespace should be xxx.yyy.zzz.
Example:
Folder1
Folder2
MyControl.cs
I have a namespace in it defined as:
namespace CustomControls
{
...
}
so the compiler is complaning that it must be namespace Folder1.Folder2
so is there a direct relation to file structure and namespaces? Are you forced to have a tight relation to these?

This is a warning Resharper usually reports. To exclude a folder from Resharper's folder tree - namespace comparison, go to the folder properties list in VS and set Namespace Provider to false.

I don't know whether ASP.NET has some special rules (due to automatic compilation) but certainly in C# itself there are no rules saying you have to organise your folders to match your namespaces. It's a good idea from a maintainability point of view though.
Are you sure it's the compiler and not just another bit of ASP.NET (or even ReSharper?) complaining?

No. You can have any namespace tree you want regardless of your folder structure. Visual Studio makes it easy on you to automatically create namespaces based on folder structure so you don't have to maintain namespace trees yourself. But does not force it in any ways shape or form.
The error you're getting doesn't have anything to do with your compiler.
You also don't have any tag saying what Dev IDE you're using.

That has to be an error coming from ReSharper and not from Visual Studio's compiler. Right now I am actually having the opposite problem: I am trying to get VS2008 to automatically name my namespaces based on my nested folder structure like Folder1.Folder2.MyProjectName but it insists on naming it "flat" MyProjectName! So as Jon said, C# in itself is agnostic to namespace nomenclature.

Related

Recurring namespace errors, but project compiles

I've been having a recurring namespace error inside one of my projects. All the references that are having issues are inside the same project and appear to be properly namespaced. Clearing some VS cache files appears to fix the problem temporarily (AppData\Local\Microsoft\VisualStudio\12.0 and AppData\Roaming\Microsoft\VisualStudio\12.0). This isn't a show-stopper by any means; it just means I can't use intellisense or code navigation.
This is especially annoying because I have two files in the same folder referencing the same namespace, with the code content in those files namespaced identically, but VS cannot find the namespace I want in one file but it can in the other. E.g....
using System;
using ExampleNamespace.Model;
namespace ExampleNamespace.DAL{
..... code content .....
}
Is the same from one file to the other. The only difference is in one file, it cannot find ExampleNamespace.Model and all the types in the namespace are not valid.
Other things I've tried:
Resetting the target framework
Build Action on files is set to compile
Deleting cache files (as above)
Opening/closing VS
I've searched high and low for an answer, but haven't found one. My main question is why the project compiles at all if the IDE can't find a reference? Doesn't the IDE essentially pseudo-compile the code to do intellisense/syntax and bug high-lighting?

I need help understanding how C# projects are organized in Visual Studio

I've been teaching myself C# for the past couple weeks, and as someone whose IDE of choice is Notepad, I'm having a little bit of difficulty transitioning to Visual Studio (I'm using 2010 express). In particular, I'm wondering how the organization of the Namespace-Class-Method hierarchy manifests itself VISUALLY in the interface. I'm having a hard time making sense of it, and, more importantly, how to use the interface to effectively organize and keep track of my projects.
For instance, there's the "solution explorer", but there's no such thing as a C# "solution" (that I'm aware of). I suspect it's Microsoft's marketing speak for a more generic development term, but I can't figure it out. I get the option of creating New "projects". Is a "project" a "solution"?
I'm also a little fuzzy on namespaces. I suspect that the namespaces are the equivalent of a class library in Java. What are some examples of how namespaces are used in the real world? Say, for instance, I'm developing a personal finance application. Would I put EVERYTHING related to that application in one solution? Or would I create as namespace for, say, cash accounts and a namespace for investment accounts?
Within the namespaces are my *.cs files but I can't seem to figure out how to create a NEW *.cs file in my namespace. I would EXPECT, based on the explorer hierarchy, that any class using a namespace would appear in that list, and I would be able to use it as needed. For instance, I would be able to create enterDeposits.cs and enterWithdrawals.cs WITHOUT needing to create a new project.
I've found a couple tutorials online that tell me how to do things (like creating a new project), but without a solid understanding of the IDE's vocabulary, I'm not really sure I'm keeping everything organized as well as I could. Help!
Solutions and projects in Visual Studio are ways to organize code - they are containers used by the IDE to issue commands to the compiler and other build components as needed.
Solutions contain projects - projects contain code files.
Each project will compile to a separate DLL or EXE, a unit of deployment.
Namespaces can be spread across projects and solutions and be in different DLLs/EXEs. They are units of logical separation.
In Visual Studio, you can set a base namespace for each project in its properties. By default, every directory you create will get appended to that as part of the inner namespace. Any source code file created in a directory will by default get that namespace.
In general, namespaces are a pure code construct.
In a C# file, you have a namespace declaration - this can be any valid namespace identifier and can be in any project/solution/code file.
I do suggest taking a look at MSDN - it is a good resource for anything C# and Visual Studio.
Solution and Project Basics
Creating Solutions and Projects
Your first question has already been answered here: Visual Studio Project vs. Solution
You can find plenty of information about your send question here: namespace (C# Reference)
Great overview by Oded. RE: would EXPECT, based on the explorer hierarchy, that any class using a namespace would appear in that list, and I would be able to use it as needed. you are correct, the IDE will recognize classes in the SAME namespace.
One thing that tricks up new users is that namespaces do not inherit. If you have a namespace MyProject and another namespace MyProject.SubNamespace, the compiler will not automatically link the SubNamespacetwo. You need to specify a Using statement, e.g. "Using MyProject.SubNamespace" to let the IDE know to use the classes in that namespace.

One namespace per assembly?

Is it a general guideline to have at least one namespace per assembly?
In what case, should multiple assemblies generally share the same namespace?
Development Environment : C# and .NET
From MSDN
Assembly
An assembly is a collection of types and resources that forms a logical unit of functionality. All types in the .NET Framework must exist in assemblies; Each time you create a Microsoft Windows® Application, Windows Service, Class Library, or other application with Visual Basic .NET, you're building a single assembly. Each assembly is stored as an .exe or .dll file.
Namespace
Namespaces are not a replacement for assemblies, but a second organizational method that complements assemblies. Namespaces are a way of grouping type names and reducing the chance of name collisions. A namespace can contain both other namespaces and types. The full name of a type includes the combination of namespaces that contain that type.
The answer is -- it depends.
If your assemblies are all small components of a given project, they may not need their own namespaces if they are distinct, self-contained and all "fit" under the namespace for the overall project.
If you're building assemblies which are only tangentially related and could easily be used in a wide variety of projects, you may want to group these in their own namespace.
If you're creating a class which has similar functionality or duplicate members to an existing class in your project or the CLR, you'll want a namespace for that too.
I would suggest to let the namespace match it's file location. Try and install resharper, you will see what i mean.
I do not know of a case where assemblies should share the same namespace. Only the first part of a namespace should be the same, the name of the company or product.
See this post from Mark
And this post to tell Resharper to get around this.

why we give reference to the other projects?

When looking at a solution with multiple projects:
1) Why do we add a reference to the other project? Can't we just use inheritance?
2) After we add the reference by using Visual Studio, why do we have to add the project to the namespace system? For example: using myReferenceProject; I thought that the IDE would do that.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using myReferenceProject;
using System.Data.SqlClient;
using System.Data;
1) why we give reference to the other project? cant we just use inheritance???
They're two completely different concepts.
Adding a reference to another assembly basically says, "I want to be able to use some of this code. Please make the compiler aware that this code exists, and optionally copy it into the output directory so that it's present at execution time too."
How would you expect to use inheritance to create a class derived from some type if the compiler has no knowledge of that type?
2) after we give the reference by using the visual studio IDE why we have to add the project to the namespace system???
Because assemblies and namespaces are different concepts too. A using directive simply says to the compiler "When I refer to type Foo within my code, look in these namespaces to try to resolve it." That type could come from any of the assemblies you've referenced.
It's really important that you understand the difference between assemblies and namespaces. Even though they're often named similarly (Foo.Bar.dll often provides types in the namespace Foo.Bar) they're conceptually different.
The project is a self sufficent compilable unit, that has to compile into the valid assembly file. That's why you need esplicitly specifiy which referencies every project needs in order to be able to compile separately from others.
There is no any inheritance concept on projects level.
1) why we give reference to the other project? cant we just use inheritance?
This question makes no sense. What does inheritance have to do with project references. Can you elaborate?
2) after we give the reference by using the visual studio IDE why we have to add the project to the namespace system?
Because there's an inherent difference between an assembly referencing another assembly (which is what happens when you add a reference to the project) and the language knowing where to find a class which is what happens when you use the using directive in a file.
For example, suppose you create a class in your project called TextBox. Then in another file you want to use that class. How would the language know whether you are referring to your custom TextBox class or another one in a referenced assembly? The answer to that question is namespaces. By fully-qualifying the class name with its namespaces, you tell the compiler which class you're using.
The using directive is a way to specifying the namespace once per file instead of every time you use the class (or other classes in that namespace). So if you need to reference your TextBox class multiple times within a single file, you wouldn't want to have to write this every time:
MyCodebase.MyAssembly.MyNamespace.MyOtherNamespace.SomethingElse.TextBox
Instead, you include a using directive of the entire namespace, so you only have to write this:
TextBox

Can I set up ReSharper to rename unimported classes usages to their full names instead of importing their namespaces?

When I try to use a class whose home namespace is not imported with a using directive, a pop-up appears allowing me to choose the class (by its full name) and adds a using directive to import it.
In a project of mine I make heavy use of same-named classes from different namespaces and would prefer to specify a full name on every use.
Can I set up ReSharper to replace a class "first name" with its full name in-place instead of importing its home namespace?
Send this question to the ReSharper support team. They'll be happy to help you out. BTW, this feature is already in Visual Studio, try pressing Ctrl + . (period) and select the full name.
I don't have this problem, and it works fine for me using Visual Studio and ReSharper side by side.

Categories

Resources