1

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();
    }
1
  • 1
    yes, look at the "using" keyword. There should be some already at the top of your source code. Commented Sep 19, 2012 at 22:53

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Advice also taken on board and everything renamed :)
2

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.

Comments

1

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

8 Comments

Brilliant! Although the other suggestions worked, this was the best way forward for me.
@hshah Note that this, while it works, can lead to less maintainable code. There's a (good) reason Visual Studio uses folder names for default namespaces - it really improves the discoverability of your types later when working with the project.
@ReedCopsey - Sorry, what do you mean by "improves discoverability of your types"?
@hshah If your project gets very large, and you need to edit code for a class, having the namespace match the folder structure makes it very quick and easy to find the class, etc. If you don't do that, it can get messy over time, esp. with very large projects.
@Star It's not bad in terms of runtime or compile time - more developer productivity. And "large" is very subjective - but as soon as you start dealing with multiple namespaces and multiple class hierarchies, organization in your project is very helpful. That's why this is done that way by default - VS tries to promote good habits (mostly).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.