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
{
.........
}
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 have the following two files:
IGlobalApiProvider.cs
using System.Collections.Generic;
using Vert.Slack;
namespace Vert.Interfaces
{
public interface IGlobalApiProvider
{
List<Im> ImList();
}
}
And the corresponding implementation: SlackApi.cs
using System.Collections.Generic;
using Vert.Interfaces;
namespace Vert.Slack
{
public class SlackApi : IGlobalApiProvider
{
public List<Im> ImList()
{
...
}
}
}
Now, Intellisense is telling me that when I use IM in IGlobalApiProvider it's resolving to Im, which is defined in a file named RtmStart.cs which has no namespace declaration. When I use IM in SlackApi.cs, it's resolving to Vert.Slack.Im which is defined in the Vert.Slack namespace in a file named Im.cs. The weird behavior alerted me to the redundant definition, so I removed it and things are working fine.
However, I'm confused about why Visual Studio behaved differently in these two ways. I can tell something was scanning for the class names in a different pattern in the two situations. I can also tell that being used in the same namespace vs being used in a class that uses the namespace seems to be the trigger. What I don't know is what mechanism controls the logic behind this behavior.
Can anyone shed light on this?
Everything you see is contained in Vert.dll, which consists of one project, Vert.csproj
Link to the four files mentioned in this post as they existed at the time of writing.
This has to do with the difference between the global and Vert.Slack namespaces.
The compiler looks for the most explicit namespace with the proper class defined.
In this example, when the compiler looks for the definition of Im in IGlobalInterfaceProvider.cs, there is no namespace defined (or used) in this file that contains the class, but Im is also defined in this file - which is declared in the global namespace.
When the compiler looks for the definition of Im in SlackApi.cs, Im is found in the explicit Vert.Slack namespace, and utilizes that class.
The answer here is a similar topic and may provide more insight.
This may be related to the fact that your namespaces are in the wrong place ;-)
http://www.stylecop.com/docs/SA1200.html
This answer here gives a good explanation: Should 'using' statements be inside or outside the namespace?
When using visual studio express to create a console C# application, I found that some namespaces are automatically added at the top of code at start:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
My understanding is first directive should allow me to use things defined in System and rest three directives are for namespaces which are defined within the overall System namespace which is already referred in first directive. Then why is VS 2013 adding rest of three using directives ?
You're misunderstanding how namespaces work.
For example, say I could define two classes, in two separate namespaces:
namespace MyNamespace
{
public class One
{
}
}
namespace MyNameSpace.SubNameSpace
{
public class Two
{
}
}
And then I want to create an instance of each in another class, like this:
var one = new One();
var two = new Two();
I'd have to include two using directives in order to do that.
using MyNamespace;
using MyNameSpace.SubNameSpace;
Including only the first using directive is not enough; it doesn't automatically include classes defined in the second namespace.
The using system does not automatically include everything within sub-namespaces. The first namespace (System) does NOT bring in the following three.
I think that the way to look at namespaces is that the "." is just another character in the name that makes it easier for humans to understand heirarchies of related namespaces and as far as Visual Studio is concerned, those are two distinct namespaces.
You can treat the like this
namespace System {
//... stuff
namespace Linq {
}
...
}
Namespace of System is different from namespace of System.linq. The first line will allow you to use System.Console. But the first line alone won't allow you to use methods in namespace System.Linq.