I've been writing a set of code that I would like to use in many places. I have put it in a namesapce called Limits. Now, I add the code file to the project, put using Limits at the top of a form file that needs to use its classes (along with using System and all the standard ones I need).
When I want to use something like System.UInt32 after including System, I can just type UInt32 and it knows the correct namespace.
Is there something to add to my custom namspace that allows this, or must I include it in the forms namespace?
Edit:
using System;
using Limits;
namespace Sandbox {
public partial class Form1 : Form {
Check lim = new Check(3, 1);
}
Check is a class inside of the Limits namespace, was throwing an error however. No that I've stripped it that far down, it seems to not fail. Not sure what I changed so I will have to go back and compare versions.
You should be able to access your classes without specifying the namespace just by adding
using Limits;
at the top of your class.
One exception to this is if you have a name collision, meaning you have a class in Limits with the same name as a class in another namespace that you're using. In that case you need to specify which class you're talking about.
For example, if you have a class called Form in Limits (and you are also using the System.Windows.Forms namespace) you'd have to include the namespace when using the class:
Limits.Form = form = new Limits.Form();
You could as an alternative use an alias in your using statements:
using MyForm = Limits.Form;
...
MyForm form = new MyForm();
Sometimes, when a build fails, you'll have a situation where classes that are in a namespace in a referenced project don't show up. If that is the case, you'll want to rebuild the project that your Limits namespace is in.
Related
I'm pretty new to the whole C# thing and I'm a bit used to C++ with the header files. So I hope this isn't the dumbest question.
How can I import the MainWindow.cs into the MainWindow.xaml.cs?
I thought I could just use using MainWindow.cs
In .Net you can use any class in the same project, or in any referenced project or dll. You never need to worry about header files. The .Net assemblies themselves contain all the type information needed to use them.
Type disambiguation is done with namespaces, you should typically place types in a namespace like ProjectName.Folder I.e.
namespace MyProject.Quelldateien.Inc{
public class MainWindow{
...
}
}
In your code file you need to use the full namespace when refering to a type, i.e. MyProject.Quelldateien.Inc.MainWindow, or add a using statement at the top of your file:
using MyProject.Quelldateien.Inc
namespace MyProject{
public class MyClass{
...
}
}
Note that you should usually try to avoid using the same name for classes, since this would require using the full namespace to refer to your type. A common convention is for your name your xaml-classes MyMainView and the view model class (containing properties you bind to) as MyMainViewModel. And place this in separate folders.
Also note that some refactoring tools can add namespaces automatically, so I almost never write out using MyProject.Quelldateien.Inc, I just write my type name, and accept the namespace suggestion by the tool.
in the MainWindow.cs see the namespace. like:
namespace Quelldateien.inc
{
internal class MainWindow
{
}
}
then use:
using Quelldateien.inc;
begginers question about C#.
In every program I have to include several namespaces, like:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Why do I have to include:
using System.Collections.Generic;
using System.Linq;
using System.Text;
... since they are included with first one:
using System;
Thank you in advance!
Because nested namespaces are not included with parent one. See using directive documentation for details
A using directive does not give you access to any namespaces that are nested in the namespace you specify.
System and System.IO namespaces are different.
You can treat "subnamespace" as parent-child relationship in the object model. If you have access to the "Car" object does not mean that you have access to car's wheels.
System is a huge namespace that contains hundreds of nested namespace and thousands of classes. You should specify all nested namespaces separately to state what part of the module are you interested in.
Imagine these namespaces with these classes (the last name is a class):
A.B.Class1
A.Class2
Now you have the following:
using A - allows you to refer to Class2 directly... but not to Class1.
using A.B - allows you to refer to Class1 directly but not to Class2.
If you want to refer to both classes directly in your code, you need both usings.
System and System.Text are two different namespaces. That System.Text seems to be a part of System is the semantics we as programmer put into it. There's no such thing as a nested namespace from a platform view;
But even if that was not the case what should happen if you had
namespace MySystem{
namespace Foo{
class Bar {...}
}
class Bar{...}
}
using MySystem;
class MyClass{
private Bar _myBar; //Which one is it MySystem.Foo.Bar or MySystem.Bar?
}
Since you're beginner let me clarify one thing namespace in C# and package in Java are different things. no need to merge them.
Not all classes in .net is inside one big container.Doing so increases the chance of class name collision and it doesn't look good in the first place.Namespaces are containers that try to keep your library clean and make more sense.Having a FTP class and String class together under one container does not logically make any sense.They do two different things and they should be kept in separate containers.
Also a namespace can be nested.Sometime A namespace can all but have just another namespace,without any class.so to access a class you need to qualify the full namespace before you can use it.
In your case you use different classes ,which are on different containers.So if you need them you need to qualify them with their namespace.
Just an added example to make you understand clearly.
A child Namespace cant be accessed by calling parent namespaces
Namespace Main //only code and classes are accessible
{
//code for Main Namespace
Namespace subMain //only code and classes are accessible
{
//code for subMain
Namespace verySubMain //only code and classes are accessible
{
//code for verySubMain
}
Namespace otherVerySubMain //only code and classes are accessible
{
//code for otherVerySubMain
}
}
}
If you wanted access to a class in System.Text without the using statement, you would have to Text."Name of class/function" every time you wished to use it.
This way you can just call "name of class/function".
Not sure if you are a beginner, but that's the simplest way I can describe put it.
I'm new and I don't know other way to explain so I posted my screenshot of project! Please help me to fix these errors... SCREENSHOT
You seem to be having a lot of problems with references and namespaces use.
First of all, you do not have a Card class defined. You only have a CardModel. Replace Card for CardModel and you will be good to go. Also, it seems you do not have a namespace declared on your class. Declare a namespace so you can use other classes in the same namespace (tipically the project name).
Second, if you are trying to use clases in another folder, you probably have to add the reference with the using keyword.
You're missing probably several using directives. Every class you write should be inside a 'namespace' You declare it after your using directives but before you start writing your classes, like this:
namespace WebShop.CardModel {
public class CardModel {
public string InsertCard(Card card){
And when you are working in the cardModel, unless Card is defined in the same namespace, you need:
using WebShop.Card;
Or whatever namespace you put Card in, that's what is throwing probably 99% of your errors, it is definitely the cause of all but one of the ones in the errors we can see in your screenshot.
I am very new to c# and this is probably a very n00b error.
For this project I have been handed existing code to work with. The structure of the code is that it has a main solution with simulation as a supporting namespace.
I copied one of the classes (Adt_12) from simulation namespace that I want to modify and renamed it (Pb_cs2). The way I copied is, was to click on save as.. and then changed the file name to the new name I want. And then changed the public class name (and the constructors) to this new file name. I have rebuild 'simulation' and it rebuilts fine.
But when I try to call Pb_cs2, it is throwing the above 'the type or namespace named Pb_cs2 could not be found'.
The way I am using it in the executable class in main; is
public static Pb_cs2 pb; (which was originally using Adt_12).
But it can still find Adt_12 in the solution and namespace. Just no Pb_cs2. I have rebuilt and built the solution.
The common error of .NET framework is not relevant.
Any ideas why this is happening and how I can fix this? I really dont want to modify the original file.
Take a look here. Visual Studio saying name doesn't exist in current context
You need to make sure:
Your class name and namespace are not the same, like Pb_cs2.Pb_cs2 as this will confuse the compiler
You can fully qualify the path to the class i.e. MyNamespace.MyNestedNameSpace.MyClass
You can use a shortcut i.e. using MyClass = MyNamespace.MyNestedNamespace.Class1
Ensure that your projects are targeting the same framework i.e. .NET 4.0 / .NET 4.0 Client Profile.
You might have a collision where your class has the same name as another class, in which case, use option 2, or rename your class to something else.
If your class name does not appear in intellisense, then it does not know where to look for it. You can right click the class and click "Resolve" which will give you some options on how to qualify your class.
...that is all I can think of right now!...Good Luck!
Edit:
Look up C# stylistic conventions... those class names are ugly!!!
Add a reference to the namespace which contains the class you are calling. So you might have something like
namespace SomeNamespace
{
public class Pb_cs2
{
...
}
}
so you need to add using SomeNamespace; to the declarations at the top of the file that is attempting to call your class. Or call the class using the fully qualified name
SomeNamespace.Pb_cs2 pbcs2 = new SomeNamespace.Pb_cs2();
You can also create a alias to the namespace when you reference it like
using NS = SomeNamespace;
then the above explicit reference can be called like
NS.Pb_cs2 pbcs2 = new NS.Pb_cs2();
I hope this helps.
Do it this way to be sure the calss is known by your solution.
Project->addclass
select class if it isn't selected by now.
Name it and then add the new class.
it should appear in your solution explorer.
Now copy paste the code. rename the class the namespace should be fine.
and you should be okay with that.
I've been working on an application and I've run across an unusual error that I've never seen before. I have two classes, one being a UserControl and one being a Form, set up like so:
namespace NC
{
public partial class Board : UserControl
{
// stuff
}
}
namespace NC
{
partial class Board
{
// this is the *.designer.cs file
}
}
namespace NC
{
public partial class MainForm : Form
{
// normal form stuff
}
}
namespace NC
{
partial class MainForm
{
// this is the *.designer.cs file. it has a Board added to it.
}
}
It's all contained in the same project so it shouldn't be an issue. However, I get this when I compile:
The type name 'Board' does not exist in the type 'NC.NC'
I looked in the Form's designer file and found this:
this.Board1 = new NC.Board();
I remove the namespace and it works fine. Every time I access the forms editor, it adds it again. I've never had any issue with this before. Is there some kind of setting or something I can change?
Do you have a class named NC? If you do, you'd do well to rename it to something else. It sounds like the compiler is looking for Board inside a class named NC, not the namespace NC...
Your project settings page have a default namespace. Take a look there.
In fact, the default namespace name shouldn't be causing this behavior. For example, I just created an empty Windows Forms app and its default namespace is set with WindowsFormsApplication1, that is the name of the Solution inside VS.
If you are using a namespace to access a class you either need to use the full namespace (which is probably ProjectName.NC.Board) or it has to be deeper than any namespace you have access to.
Board is in the same namespace that you are already in, so you don't need to qualify it with the namespace.
If you're calling [namespace].[class] from within its own namespace, so C# assumes you're looking for a class called NC even though you don't have one. That is, it is automatically appending NC. to anything you're calling that isn't referencing an outside namespace (System.[whatever]). So your call to NC.Board gets translated to NC.NC.Board