c# namespace in variable directories - c#

I have a directory tree as such:
+Enums
|+MyEnums.cs
+Src
+Model
+MyModel.cs
And I would like to use the namespace Enums that I declared in MyEnums.cs in the file MyModel.cs. But I cannot figure out how to do such, because when using the code
using Enums;
I constantly get the error/warning that
The type or namespace name 'Enums' could not be found (are you missing a using directive or an assembly reference?)
If I use it in the same folder I have no issue, but I would like to use it across a larger directory tree.

The directory structure of your project and the structure of the namespaces are technically two unrelated things; nevertheless it makes sense to have them organized in parallel, but still they are orthogonal concepts. You have to define a namespace by putting types into it as follows.
namespace Enums
{
// some definitions
}
namespace Enums.MyEnums
{
// some more definitions
}
After populating the namespace, you can import the namesepace with the using directive.

Adding a file to a directory like that typically gives you a namespace that is the same as the directory name, but that is not necessarily the case (such as when you move a file from one folder to another). The namespace is determined like this in the file. Check what the namespace is and change it to Enums if that's what you want it to be:
using System;
namespace Enums
{
enum Testing
{
Test1,
Test2
}
}

Related

Referencing type in different namespace with same prefix generates CS0234 error

I'm confused why the following C# code won't compile, and instead generates the following:
error CS0234: The type or namespace name 'Module' does not exist in the namespace 'SomeName.Module.SomeName' (are you missing an assembly reference?)
using System;
namespace SomeName.Module.AnotherName
{
public struct SomeStruct
{
}
}
namespace SomeName.Module.SomeName
{
public class SomeClass
{
struct SomeNestedType
{
void SomeMethod(in SomeName.Module.AnotherName.SomeStruct someStruct)
{
}
}
}
}
I'm not aware of any namespace rules that would prevent it from finding the fully-qualified SomeName.Module.AnotherName.SomeStruct type.
The problem is that SomeName is being resolved as the final part of the namespace SomeName.Module.SomeName, and Module is being resolved relative to that.
If you absolutely can't change your namespaces, you can use the global namespace alias to disambiguate:
void SomeMethod(in global::SomeName.Module.AnotherName.SomeStruct someStruct)
However, it would be better to just use different names. For example, if you have namespaces of SomeName1.Module.AnotherName and SomeName1.Module.SomeName2 you don't get this problem - and the code will be easier for humans to read as well as the compiler. (Obviously it wouldn't actually be that name, but I can only hope that you're not really using SomeName in the first place. Pick good but different names for the two parts of the namespace.)

Quick check regarding C# namespaces

Okay, there's System and System.Web. Am I correct in that the structure this suggests is:
namespace System
{
// all of the outer namespace members
namespace Web
{
// all of the inner members
}
}
And that when a namespace is nested within another, having a using directive with the parent/outer namespace only doesn't automatically bring in the child/nested namespace? In other words:
using System;
public class Example
{
public Example()
{
context1 = new HttpContext(); // won't work
context2 = new System.Web.HttpContext(); // will work
}
}
Just trying to see if I actually understand this correctly.
System.Web is declared as:
namespace System.Web
{
public class HttpContext {}
}
However, it would be possible to actually declare a child namespace:
namespace System
{
namespace Web
{
public class HttpContext {}
}
}
I have never seen something like this but the syntax allows it and the effect is the same. In both cases, the namespace of HttpContext is System.Web.HttpContext.
Even with the second example, using System; wouldn't import the child namespace, only the types defined in that namespace are imported.
You could nest namespaces and any using directive would only grant access to the members defined within the specific namespace you are referencing.
So from your example:
namespace System
{
// all of the outer namespace members
namespace Web
{
// all of the inner members
}
}
Referencing System would grant you access to the outer namespace members and referencing System.Web would grant you access to all of the inner namespace members.
But this is atypical and usually namespaces are defined only once within a file. The dot-notation typically follows a folder or project structure, so files that were nested as such:
WebApplication
- Models
- MyModel.cs
- Controllers
- MyController.cs
Might use namespaces of WebApplication.Models and WebApplication.Controllers.
I can't think of a great example off the top of my head where you would want to nest namespaces, but there may be a good reason to. However, it would be considered an exception to the rule, in my opinion.
Yes, a using directive only allows types declared in that namespace to be used without namespace qualifier. Nested namespaces are not automatically included.
As Daniel said, System.Web is not declared separately. System and System.Web are two separate namespaces which are technically unrelated.
That's why your code example of new HttpContext() won't work - because HttpContext is not in the System namespace at all.
this is a common confusion regarding composite namespaces. Heres a great article of microsoft about it: http://msdn.microsoft.com/en-us/library/ms973231.aspx

using directive to import subnamespaces

If I import a namespace like this:
using System;
Why can't I access subnamespace IO like this:
IO.FileInfo fi;
Insted I must write either a whole path:
System.IO.FileInfo fi;
Or import whole IO namespace and use class without namespace
using System.IO;
FileInfo fi;
Am I missing something here?
While it's often convenient to think in terms of "namespaces" and "sub-namespaces", in reality, there are only type names.
In this case, there is a single type: System.IO.FileInfo
The using directive allows the compiler to add System. to any type to see if it finds a matching type name. However, this won't find IO.FileInfo, as it will be looking for a IO type, containing a FileInfo nested type.
The way the language is designed may seem more cumbersome, but it eliminates the confusion of nested type names vs. namespace names, since it only looks for types within the namespaces defined in the using directives. This reduces the chance of type naming collisions.
C# does not really have the concept of subnamespaces. The periods in the namespace name are just there for logical organization purposes.
System and System.IO are two different namespaces as far as C# is concerned.
If you just need the FileInfo class you could do this:
using FileInfo = System.IO.FileInfo;
What you are trying to do only works if the context type is in the same namespace hierarchy, e.g.:
namespace System {
class MyClass {
IO.FileInfo fi;
}
}
You can also have relative imports, like this:
namespace System {
using IO;
}

How to assign same class object declared in 2 different namespaces

I have a webservice project with a class (let's refer to it as webservice.classA).
I have another class project producing a dll which references that class in its own namespace and instantiates an instance of it (lets call the dlls namespace dllnamespace).
In another project I want to access the member in the dll
e.g.
using webservice;
namespace other_project
{
class B
{
classA copy = null;
//....
dllnamespace.dostuff(); // amongst other things instantiates a classA object
//....
copy = dllnamespace.getclassA(); // method to return classA member
The compiler error I get is cannot convert type from dllnamespace.webservice.classA to other_project.webservice.classA
I guess I have a fundamental design flaw but I figure there must be (?) a way to declare/use "webservice.classA" in more than one namespace.
You have a name clash. The supported way of avoiding this (short of not naming your classes the same), is to define a using alias for one of the classes:
using webservice.classA = myWebserviceClassA;
You are right...the design flaw does exist in terms of naming.
Let us assume:
you have a class named
MyClass
the class exists both in namespace- abc.xyz.qwe.tyu.MyClass
and in namespace - sed.qwe.dfg.ert.MyClass
The workaround is -
using NS1 = abc.xyz.qwe.tyu.MyClass;
using NS2 = sed.qwe.dfg.ert.MyClass;
This way you avoid the clash.
Also, helpful to use if you have very long namespaces.
FURTHER REFERENCE : (From MSDN article on using Directive )
The scope of a using directive is
limited to the file in which it
appears.
Create a using alias to make it easier to qualify an identifier to a
namespace or type.
Create a using directive to use the types in a namespace without having to specify the namespace. A using directive does not give you access to any namespaces that are nested in the namespace you specify.
Change the copy definition line to:
dllnamespace.webservice.classA copy = null;
That's just the problem - you cannot have a class in more than one namespace. This is what namespaces were designed for - to prevent classes with the same name written by different people from aliasing. You'll need to decide for one of your namespaces to own that class and in the other one to import it. Alternatively if the dll and the web service are part of the same distributed app then they should use the same namespace.

Namespaces and Using Directives

If I have a namespace like:
namespace MyApp.Providers
{
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
}
Does this mean that if I create other files and classes with the same namespace, the using statements are shared, and I don't need to include them again?
If yes, isn't this a bit of a management headache?
No, it's only good for the namespace section inside the file. Not for all files inside the namespace.
If you put the using statement outside the namespace, then it applies to the entire file regardless of namespace.
It will also search the Usings inside the namespace first, before going to the outer scope.
You need to specify the using directive for any classes that you want to reference without qualification in each file where you want to use them.
Reference:
The scope of a using directive is
limited to the file in which it
appears.
No, it doesn't, and so no, it isn't.
Consider the fact that outside the namespace declaration, you are in the global namespace. Do using statements in that region of the source file affect the global namespace in other source files?
No. You'll need to include the namespaces in every class except on partial classes.
One side note: you're doing a very good practice of putting the using statements inside the Namespace. That's very good syntax.
Keep up the good work.
The using statements are valid for the code file in which they appear, with a minor twist; if you put the using statements inside the namespace, they are limited to the scope of that namespace, but still only within the same code file.
Usings only apply to the current file. Whether they're inside or outside the namespace declaration makes only a small difference:
The lookup order for types is as follows:
start in the innermost namespace declaration
look in the current namespace
look in the usings of the current namespace
go up to the parent namespace declaration and repeat from step 2
As a result, this program will compile fine:
namespace MyProject.Main {
using System;
class Program {
public static void Main(string[] args) {
Console.WriteLine("Hello, World!");
}
}
}
// in another file:
namespace MyProject.Console {
class Test {}
}
But if you move the using System; to the top, then the compilation will fail (MyProject.Console.WriteLine doesn't exist).

Categories

Resources