New Class Item in Visual Studio 2017 not detected

Multi tool use
Multi tool use


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:


NewClass



"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).


NewClass



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


NewClass


NewClass



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.





Have you made sure you're referencing the project where NewClass is defined in the project where NewClass is used? Also, have you made sure you NewClass was defined in appropriate namespace and that namespace was used in the file where you want to use NewClass?
– Aly El-Haddad
Jul 2 at 17:21


NewClass


NewClass


NewClass


NewClass





@AlyEl-Haddad The definition of NewClass in NewClass.cs now works (for some reason), so yes, the syntax and semantics are correct.
– samsara
Jul 2 at 17:25



NewClass





It sounds very much like an issue with your namespace naming. Check if perhaps you've made a typo in namespaces.
– Sach
Jul 2 at 17:25





@Sach I used Add > New Item on a particular folder to see if a namespace based on the folder structure would actually be generated, and it was (since consistency between folder structure and namespace is only a convention). I did this instead of copy and pasting and existing file (as usual) as a quick experiment.
– samsara
Jul 2 at 17:27






I don't know if it's your case, but when adding a class to a folder, the namespace changes from default Namespace to Namespace.FolderName. Try remove the .FolderName
– Magnetron
Jul 2 at 17:30


Namespace


Namespace.FolderName


.FolderName




1 Answer
1



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


class



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


Properties


Build Action


Compile



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





I did compare properties as a troubleshooting measure, and aside from "File Name" and "Full Path", they were all the same ("Build Action" is set Compile).
– samsara
Jul 2 at 17:31





@sαmosΛris: I see. Did you make sure the file is part of the project?
– JuanR
Jul 2 at 17:33





It would be useful if you post a code sample of the class definition, the code where the compiler breaks, a snapshot of the VS solution Explorer tree and a snapshot of the File Properties window.
– JuanR
Jul 2 at 17:35





If I can replicate the issue in a sample solution then I will add some edits. I've already spent too much time on this post.
– samsara
Jul 2 at 17:37






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

8Mr6IKqNG2KCid1msV2ySXS3ZSDsrVZYErH3,RAbAc4b4wvw,Uf vOAlEUbmqPeOU,T7,44eEvDBqYKgPNDRu7THTKX8
Qgcx3nMGedbxCYHf0wthpKiRGwnjcloVY pmSZS 4

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications