samus
samus

Reputation: 6202

New Class Item in Visual Studio 2017 not detected

Note: The newly added class/file was to a Xamarin.Android project, and referenced from another.


After adding a new class/file, NewClass/NewClass.cs, to a particular folder with the Add > New Item command, it wasn't detected by the compiler, which was indicated by the error:

"The type or namespace 'NewClass' could not be found (are you missing a using directive or an assembly reference)?

Note: I made sure the new type was made public

The project that NewClass was added to built successfully. It was in the referencing project where the error was occurring (in a separate namespace and assembly).

After cleaning the solution and first building the referenced project failed, I tried the following:

  1. Copied the definition of NewClass into an existing file and commented original one in NewClass.cs - error fixed.
  2. Switched definition back to original location by commenting out the copied definition and un-commenting the one in NewClass.cs - error returned.
  3. Copied the definition of NewClass into another existing file and commented original one in NewClass.cs - error fixed.
  4. Switched definition back to original location by commenting out the copied definition and un-commenting the one in NewClass.cs - error did not return.

Is there a manual operation that can be performed to ensure that a newly added class is successfully discovered by the compiler? Cleaning and rebuilding didn't work, nor did reopening the solution.

Upvotes: 3

Views: 1101

Answers (1)

JuanR
JuanR

Reputation: 7803

I believe you used a different template originally (other than class) and renamed, or something of the sort, so the build action is incorrect.

Right click on the file and hit Properties. In the Properties window, make sure Build Action is set to Compile.

As a last resort, copy your code, delete the file, add a new file with the same name, then paste the code back.

Upvotes: 1

Related Questions