C# Namespace issue - c#

I am new to C# and I wish to know how to use my own namespace in c#.
Suppose I have a namespace MyNamespace1.
And I have another namespace MyNamespace2. I want to use MyNamespace1. But when I use
using MyNamespace1;
it is not recognizable.I want to know how to do this.

MSDN is your friend in learning about this.
The namespace keyword is used to declare a scope.
namespaces used to organize it's many classes
namespace N1 // N1
{
class C1 // N1.C1
{
class C2 // N1.C1.C2
{
}
}
namespace N2 // N1.N2
{
class C2 // N1.N2.C2
{
}
}
}
Using Namespaces (C# Programming Guide)
Check your accessibility level of your namespace. If it is in same project then you can access it directly.
using YourProjectName.NamespaceThatYouCreated;
If it is another project like dll etc then add reference to that library or project.
access namespace as:
using AnotherProject.CreatedNameSpacename;

http://msdn.microsoft.com/en-us/library/z2kcy19k%28v=vs.80%29.aspx
and this one in particular:
http://www.csharp-station.com/Tutorials/Lesson06.aspx
Also, what I've discovered, if you're using asp.net you might need to change the property "Build action" of the class you wish to use in your page to "Compile".

If both of your name spaces are in different project then you must add the reference of the other project to access it's namespace. In your case add the project reference of MyNamespace1 to the project the project of MyNamespace2.

Related

Can't utilize an existing Class in a new C# project?

I'm a student learning mainly C++, but this term we have to code our math assignments using C#.
Our professor supplied a basic skeleton program but I'm not very good at C#. He gave us two class files (.cs) but when I add them to my project, I'm unable to utilize them at all. I can't create a class object from either class.
The classes are just Line3d and Point3d. They have the variables needed to compute points and collision.
Thanks for any advice.
Compile your project.
Use Ctrl + . or bulb icon (type your class name you want to use and locate your cursor position over that class name) to resolve namespace for these classes or write using directive manually.
C# classes are normally encapsulated in namespaces. In Visual Studio, adding a new class will generate a file containing a namespace similar to PROJECT_NAME.SUBFOLDER.SUBSUBFOLDER For example:
// MyClass.cs
using System;
namespace MyProject
{
public class MyClass
{
}
}
And then you can reference that from another class in the same namespace, but you can't reference it from a class in another namespace (unless it's a namespace that starts with MyProject.).
// Line3d.cs
using System;
namespace TemplateProject
{
public class Line3d
{
}
}
// MyClass.cs
using System;
namespace MyProject
{
public class MyClass
{
public Line3d LineInstance {get;set;}
}
}
In this example, it won't work because the compiler doesn't know which namespace Line3d exists in (and, indeed, two class with the exact same name could exist in two different namespaces). You need to instruct the compiler to include classes from the TemplateProject namespace (note this doesn't include classes in the TemplateProject.ChildNamespace namespace):
// MyClass.cs
using System;
using TemplateProject;
namespace MyProject
{
public class MyClass
{
public Line3d LineInstance {get;set;}
}
}
Now you should be able to find the Line3d class and use it.
Besides manually referencing the namespace, you can also right-click an unknown class reference, select "Quick actions and Refactorings...", and then you will see something like "using TemplateProject;". Click on this and it will automatically add the using for you.
You can also use the Ctrl+. keyboard shortcut, which does the same as right-click/Quick actions, if you don't want to use the mouse.
select your project and press [ Shift + Alt + A ] to add existing files.
you can see dialog form that allows to open cs file on project.
After that, you can use professor's class files.

C# using statement application scope

I have a question about the using statement for multiple files at once.
I have created an overload for a class that I want to use in my program.
To do so in one of my files I have added the following using statement.
using ClassName = CustomClassName;
This works, but only for that particular file.
Is there a way to get this to work for my entire project?
No.
using directives are per file.
You can always create a template that includes it.
No; C# does not have any such feature.
This is called type aliasing, re-utilizing the using keyword may be confusing, but it is not an import.
The purpose of this statement is to make a certain class accessible via a different name. This is useful in cases when you have different assemblies linked to your project which accidentally have classes with the same name.
If you for instance have A.dll that defines class Foo under the A namespace, and a B.dll assembly that also defines a Foo class under the B namespace, you can use:
using FooA = A.Foo;
using FooB = B.Foo;
to make distinctions between both.
The scope of this is usually the current file, although, if you happen to define multiple namespaces in the same file, you can scope that to the namespace within the file:
using FooA = A.Foo;
namespace N1
{
// knows about FooA;
using FooB = B.Foo;
}
namespace N2
{
// knows about FooA
// does not know about FooB
}
Practically you can make this aliasing more defined but no broader than the file's scope.
A simple solution for this is to create a template and use it into you project or class. this will give you a way to use the desire usings and directive as you wish.
here is a good sample to create a template .
http://www.rhyous.com/2010/02/17/how-to-modify-the-default-new-class-template-in-visual-studio-2008/
http://visualstudiomagazine.com/articles/2008/09/01/define-your-own-item-templates.aspx

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

C# "linking" classes

I'm starting a simple C# console solution via Mono on Mac OS X.
I have a Main.cs file for starters, but I want to create a separate class and be able to access object of that class from my Main.cs file.
How can I access that class from the Main.cs file?
Say my class name was Math.
In my Main.cs file, can I create a new object like so:
Math calculator = new Math()
Without referencing the class in the Main.cs file in any way?
Or, do I have to use some sort of import statement/directive?
You need a using statement if your Main and Math are in different name spaces, otherwise it just works. Below is an example. The using System brings in the library that contains the Console class, but no using is required to use the Math class.
Program.cs:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Math caclulator = new Math();
Console.WriteLine(caclulator.Add(1, 2));
}
}
}
Math.cs:
namespace ConsoleApplication1
{
class Math
{
public int Add(int x, int y)
{
return x + y;
}
}
}
There are two scenarios here. Either this class is in a separate dll (class library project), or under the same project. To reference within the same project not additional work is needed, other than referencing it with the correct namespace (as mentioned in other posts).
In the case of a separate dll, you need to add a refence to the project in the project definition. Most default projects come with a reference to System.dll and other related libraries. It is recommended to name your dll's based on what namespaces are defined within it. If you have classes like Foo.Mathematics.IntMath, Foo.Mathematics.DblMath then I suggest you name it Foo.Mathematics.dll.
When I was where you are, I picked up .NET Framework Essentials from O'Reilly and it had answers to all my questions at the time.

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.

Categories

Resources