Referencing folders created in the project

Asked

Viewed 1,770 times

4

After I go to Project > Add > New Folder, rename the folder and put classes inside it I can no longer reference it in another file, for example if I would use in one controller, I would use as I always used the command

using nomeProjeto.NomedaPasta

However, this "Folder Name" does not appear and even putting without the help of VS, when I use the controler that has the reference, is generated an exception saying that the namespace is invalid.

I have tried everything, close and open VS, close all VS Tabs and open again, clean the temporary file, restart the machine...

2 answers

6


I may be wrong, but I don’t think you understand what you have to use in this using. He refers to namespace, not to projects and folders.

To use classes that are in nomeProjeto.NomedaPasta, they must be inside a block like this:

namespace nomeProjeto.NomedaPasta {
    //classes aqui
}

I put in the Github for future reference.

But probably not what you want. Don’t mix namespaces with folders and projects, they are separate things. These namespaces must organize the code. Visual Studio organizes the project. These are things that have no direct relation.

I cannot suggest a more suitable name for the namespace because I don’t know the nature of the software and the structure of the code.

More information on the subject. Other question about this.

  • About the concepts, I was already aware about the differences, I think I expressed myself badly. However visualizing his answer and the gypsy I was able to locate the problem, when creating a class in the folder, VS did not automatically introduce the namespace...

  • 1

    Especially since it’s not his job to do this.

2

I think there’s a confusion of concepts there.

This:

using nomeProjeto.NomedaPasta

Indicates the use of a namespace, not a directory.

What Visual Studio does for you (for convenience, by the way) is, when creating a class in a directory, designate a namespace for the class based on the directory name. So much so that nothing prevents you from creating a class within one directory with another namespace. Will work just like.

  • 1

    About the concepts, I was already aware about the differences, I think I expressed myself badly. What was wrong here, is that by sla which reason, circumstance reason, the VS was not automatically designating the namespace in question, I changed manually and worked perfectly!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.