1

I'm using VS 2017 v15.5.0.

I have a minimal Console project named, Con_02. The namespace for the main class in this project is simply Con_02 (the class is shown below).

Within this project I add a new folder named, Business. Within the Business folder I create a class named, Employee. The default namespace generated by VS for the Employee class is Con_02.Business. I simplify this namespace to Business.

Back in my main class I instantiate Employee. Here is my full main class:

namespace Con_02 {
    class Program {
        Business.Employee e1 = new Business.Employee();

        private static void Main() { }
    }
}

So far, so good. Everything compiles.

Now, I create another class, Company, in the Business folder. VS generates a namespace, Con_02.Business.

Now, the main Con_02.Program class no longer compiles. Specifically, the creation of the Business.Employee object which had previously compiled just fine, gives me a compiler error:

The type or namespace name 'Employee' does not exist in the namespace 'Con_02.Business' (are you missing an assembly reference?)

I'm not asking how to fix the problem so much as I'm trying to understand why compiler seems to assume a namespace relative to Con_02.

2
  • 3
    And that's why you don't want duplicate namespace names. Try global::Business.Employee Commented Dec 6, 2017 at 20:26
  • Either add using Business or global:: as suggested above. But better just don't use such namespaces. Commented Dec 6, 2017 at 20:30

2 Answers 2

1

Since you are creating a new namespace called Con_02.Business which contains Company class the Business.Employee is considered to be under Con_02.Business namespace but Con_02.Business contains nothing but Company class.

enter image description here

Better to change

namespace Con_02.Business
{
    class Company
    {
    }
}

to

namespace Business
{
    class Company
    {
    }
}

or just use Employee e1 = new Employee();

Remember namespace is only about grouping the classes.

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

2 Comments

Yea, I know what's happening and how to "fix" it but it seems odd that the compiler assumes Employee must be in the same namespace as Company. I don't like it.
So just use Employee e1 = new Employee(); then
0

Add

using Business;

at the top of you Program class.

and remove The Business-part from your Employee instantiation.

OR. Use the same namespace for all you classes.

Comments

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.