简体   繁体   中英

Namespace Not Found for same namespace

I have two same project in which I have two classes under same namespace in one project. In the second project I have added reference to that project and have specified using statement in second project to use namespace (classes under that) of first project.

The problem is it allows me to use 1 class but doesn't allow me to use second class.

?????Why????

Here's the code:

Project 1

using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LibraryProject
{
    public class test
    {
        public static string ErrorMessage = "";


        public test(string code)
        {
           ErrorMessage = code;

        } 


    }

    public class FirstClass
    {
        public static string ErrorMessage = "";

        public RFIDHW(string code)
        {
            ErrorMessage = code;

        }

    }
}

Second Project

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using LibraryProject;

namespace Engine
{
    /*
     * Connect engine logic with back end DB and other library
     */
    public class BackEnd
    {

        //Return tag list from the connected reader as List<Tag>
        public string GetMessage()
        {
            FirstClass fc = new FirstClass();  //OK no problem

            test tt = new test(); // Error message

        }//End of GatTagList method

    }//End of BackEnd class
}//End of namespace

Error message that I am getting is

The type or namespace name "test" could not found (are you missing a using directive or an assembly reference?).

I have added reference to second project in first project.

I have tried same mechanism in new solution just by taking that piece of code and it worked. (unfortunately I can't move by whole project so have to fix this.)

Please help

Thanks a lot in advance

Not that I'm certain this is the cause of the error, but the first thing I notice is that your test constructor asks for a parameter:

    public test(string code)
    {
       ErrorMessage = code;

    } 

But you are calling the default (parameterless) constructor here:

test tt = new test();

That wouldn't work, because you created a constructor so the default constructor is no longer available. You need to pass in some string so that your declared constructor ( test(string) ) gets called.

Try building each assembly separately, sometimes there are build errors that are not shown on a build all command because of the build order.

You should also set the build order so the first project is always built before the second.

You say: "I have added reference to second project in first project.", but it should be the other way around. You need to add a reference to the first project in the second project since you are using a class from the first project in the second project.

Cannot reproduce the compiler error you got, but a few other compile-time problems:

  1. class test has no parameterless constructor.
  2. method RFIDHW in FirstClass has no return type specified. if it is supposed to be a constructor then you forgot to rename it.

To get that error you got you'd have misreferenced somehow. Recheck that your Second assembly has refence to the first assembly (not vice versa as described in OP).

I dont see any problem using this code, except RFIDHW() and parameters. Once you rectify this, you should be able to run it. If it does not, check your application if your Namespaces are conflicting typename, or if any library you are using has conflict with your class.

I had a similar problem with a class that VS17 couldn't find (error was reference or assembly missing), and it was in the same namespace as the other class.

My problem, I think, was because i pulled some changes from GIT , cause I work on 2 different places. I had moved the "missing" File.cs to another folder and through .gitignore the VS17 didn't get the information that sth. changed.

The (not so great) solution was to create a new class with command ctrl + . , and after that VS17 will find both of your classes, then delete the unwanted one..

Hope no one will encounter that problem...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM