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;
Related
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 have a doubt in creating class library in .net. I saw in one class library code that they didn't use the namespace in it. But still its compiled and run successfully when I use the method in other classes. So can you please tell me what is the advantage, disadvantage of it?
Is there any specific reason to not mentioning the namespace in class library?
The choice is yours, both can be used.
If you do not use namespace then you have to retype the namespace names repeatedly in every call function, so it is not efficient because it makes the function name too long.
Types are organized into namespace to avoid naming conflicts and make type names easier to find.
Example:
A. Without namespace
Namespace + method (it's long and namespace are always used repeatedly)
System.Security.Cryptography.RSA rsa = System.Security.Cryptography.RSA.Create();
B. With namespace
Namespace (once declaration, on top of class)
using System.Security.Cryptography;
Method (it's simple)
RSA rsa = RSA.Create();
Namespace is like a book shelf in library you can store the books in the shelf so when you want a specific book you know where to look i.e imagine you need two classes in code like Home.cs and about.cs
now if they dont have namespace you have to do something like
using yourFolderPath.home
using yourFolderPath.about
now if you have a name space then
using namespace
Plus namespace become morre importent once you jumps to n-tier arch where you have a different Layer for your DataComponent ie models
I believe the OP is asking about the use of the "namespace" keyword in the class library file itself. If your class library file only contains a single class, the namespace keyword isn't necessary. You can just have your using statements at the top of the file and then the class definition itself, not enclosed in a namespace. If your library file contains more than one class definition, it's best to enclose those definitions in a namespace.
Example -- Library file with a single class definition:
using System;
// Additional using statements here
public class MySingleClass
{
// Class definition here
}
Example -- Multiple classes in a library file:
using System;
// Additional using statements here
namespace Vehicles
{
public class Car
{
// Class definition here
}
public class Boat
{
// Class definition here
}
public class Airplane
{
// Class definition here
}
}
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.
I have organised my project with separate folders for groups of classes, but now in order to get to any method I have to reference the whole path like:
Classes.Users.UsersClass.Get();
Classes.Database.ConnectionClass.Test();
if (!Classes.Database.UsersMethods.Authenticate())
{
Classes.Users.UsersClass.LoginFailed();
}
As you can see, this is going to get messy after a while, so is there a way I can just call the class directly?
/Edit
This is the fixed up version:
Users.GetWindowsUser();
Connection.Test();
if (!UserMethods.Authenticate())
{
Users.LoginFailed();
}
You can add a using directives at the top of your C# file:
using Classes.Users;
using Classes.Database;
This would then let you type:
UserClass.Get();
ConnectionClass.Test();
That being said, I would strongly recommend not using "Class" as a suffix on every class, and also recommend not using a namespace named "Classes". Most things in C# are classes - there is no need to suffix every class with this in terms of naming.
For details, please refer to the Namespace Naming and Class Naming guidelines on MSDN.
Add the appropriate using statement to the top of your file. E.g. using Classes.Database;
Also, in VS 2010 if you just type the name of the class without the namespace (e.g. ConnectionClass) then hit ctrl+. it will give you the option to automatically add the appropriate using statement.
you can simply put using directives on the top of the file, or
if you don't want the classes to be in separated namespaces go to the class file and change the namespace to project original namesapce
namespace myProject.SubFolder
{
.......
}
will be
namespace myProject
{
.........
}
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.