Question

"Type 'HR.Database' is not defined'

I get that error for several classes that are stored in the App_Code folder in the Error List panel when building my project.

It is a class I wrote with namespace HR

How can I reference it or alter the project to recognize those classes and remove the error?

I thought I should reference it but when trying to add a reference there are no items in the list of projects. Also, they are .vb files and not a .dll

Was it helpful?

Solution

I wasted the better part of a working day on this problem before figuring it out...

When you add files to App_Code, Visual Studio sets the "Build Action" property to "Content", instead of "Compile" which it does for all other classes. So your classes don't get compiled, and then VS complains that it can't find them. Presumably this is VS' way of saying that App_Code is not where you should put your app code, reasonable as it may appear.

The solution is to right-click on each file and reset "build action" to "compile". This also has to be done whenever you add files. Or you could submit to VS and put your source somwhere else.

< /frustration>

OTHER TIPS

Since it's late and over 3 years old. I came across a similar issue. The solution above is perfect.

With newer VS IDE versions, go to App_Code file's properties and update the Build Action to 'Compile' if it's set as 'Content'. Using App_Code folder may be outdated in newer VS versions but some older developers still use the folder.

Replace vbproj and vbproj.user with your backup

My issue wasn't the App_Code folder in particular, but I was getting "not defined" errors in my Errors List until I double-clicked to open the file, then references would resolve and that error would disappear without me doing anything more, but when I tried to rebuild, all these errors would appear again.

The only real clue I had was that they were all related to the same project and when I dug through my Output window, I could see there was a conflict on the .Net framework versions. Once I resolved the version difference, the errors disappeared.

I was getting this issue for a slightly silly reason, but it might help for others to learn from my pain. :D

I was making the changes to the code in a tab of my test program, but that tab was for a VB file in a DLL I was using. Since those changes weren't being made/compiled in the DLL, it wouldn't find the class no matter how hard I tried until I opened the DLL project, made the changes there and rebuilt the DLL.

I had the same issue in Visual Studio 2019 but the .vb file already had "Compile" in properties > build action

I had tried rebuild, clean, deleting the file.

If I copied an existing class into this folder it would also not be recognised

Closing visual studio and restarting it fixed the issue, so there's a bug in VS where it doesn't compile files in a new folder until VS is restarted

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top