hshah
hshah

Reputation: 841

C# Using Classes in Project

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();
    }

Upvotes: 1

Views: 575

Answers (3)

Alaa Jabre
Alaa Jabre

Reputation: 1903

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
{
.........
}

Upvotes: 1

Dylan Smith
Dylan Smith

Reputation: 22255

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.

Upvotes: 2

Reed Copsey
Reed Copsey

Reputation: 564841

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.

Upvotes: 2

Related Questions