No namespace specified in which I want to access another class. RRS feed

  • Question

  • User-1694097019 posted

    There is no existing namespace declaration. The problem that I am now facing is that I don't know the namespace associated in this project. And even if I am going to create a namespace declaration and rebuild the project, it still won't work.

    Usually, whenever you create a class, the namespace declaration will also be included, but in this case there is no namespace included. If I am going to add a namespace it will not have any errors but I can't still access the other class.

    I hope you can help me with this. Thanks.

    Tuesday, June 27, 2017 6:19 AM

All replies

  • User-821857111 posted

    In the main VS menu, click on Project -> Properties to see the default namespace. Note: if you don't have a "Project" option in the menu, but you have a "Website" option, then you have created a Website project instead of a web application project. You need to convert that to a web application project (or start over) if you want to use MVC. Website projects do not support MVC, They only support Web forms and Web Pages applications.


    Usually, whenever you create a class, the namespace declaration will also be included

    It's usually the default namespace concatenated to the folder name that the class is in. If the class is placed in a hierarchy of folders, you will see that as the namespace, so ifyour default namespace is WebApplication1, and you add a class file to a folder called MyStuff in the Models folder, the generated namespace will be 

    namespace WebApplication1.Models.MyStuff

    If you want that class accessible to other classes in other namespaces, you add the following to the top of the other class file:

    using WebApplication1.Models.MyStuff;

    Or you can simply remove the folder names from the namespace in the class file you created so that the class resides in the default namespace:

    namespace WebApplication1

    That makes it accessible to all other classes in the application without having to add using directives.

    Tuesday, June 27, 2017 1:00 PM