Recurring namespace errors, but project compiles - c#

I've been having a recurring namespace error inside one of my projects. All the references that are having issues are inside the same project and appear to be properly namespaced. Clearing some VS cache files appears to fix the problem temporarily (AppData\Local\Microsoft\VisualStudio\12.0 and AppData\Roaming\Microsoft\VisualStudio\12.0). This isn't a show-stopper by any means; it just means I can't use intellisense or code navigation.
This is especially annoying because I have two files in the same folder referencing the same namespace, with the code content in those files namespaced identically, but VS cannot find the namespace I want in one file but it can in the other. E.g....
using System;
using ExampleNamespace.Model;
namespace ExampleNamespace.DAL{
..... code content .....
}
Is the same from one file to the other. The only difference is in one file, it cannot find ExampleNamespace.Model and all the types in the namespace are not valid.
Other things I've tried:
Resetting the target framework
Build Action on files is set to compile
Deleting cache files (as above)
Opening/closing VS
I've searched high and low for an answer, but haven't found one. My main question is why the project compiles at all if the IDE can't find a reference? Doesn't the IDE essentially pseudo-compile the code to do intellisense/syntax and bug high-lighting?

Related

Referenced namespace not being recognized

I cant get the namespace system.runtime.caching to be recognized despite adding the reference. Based on research to similar problems, I've checked the target framework and all seems in order (currently 4.5.2).
Has anyone got a suggestion on how to solve this?
Probably the problem is you are putting your code in App_Code folder. Files in this folder are treated as content by default. You can solve this issue setting Build Action to Compile for .cs files in this folder.
However, you shouldn't use this folder for code, it will surely cause problems. I recommend you to move your code to another folder Infrastructure or Code.
I think you may have to add a reference to System.Core in your project. See these links, particularly the first one:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/804fdc52-1082-4f93-b256-902ab300d78d/problem-with-namespacesystemruntimecaching?forum=vbgeneral
The type or namespace name 'Caching' does not exist in the namespace 'System.Runtime'
Have you tried restarting Visual Studio? I added the reference and I was able to use it as a namespace.

Compiler Error Message: CS0246 [duplicate]

I have written a class called ArchivedFilesWrapper in the App_code folder of my project, however when I use this class in another file in a different folder i get error:
The type or namespace name 'ArchivedFilesWrapper' could not be found (are you missing a using directive or an assembly reference?)
I thought every page should be able to find classes that are contained within the same project, but I guess this is not the case. Can someone please tell me what using statement I need to have?
Here is a snippet from my class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace EMCWebAdmin.App_Code
{
public class ArchivedFilesWrapper
{
Perhaps the problem will be solved by changing the Build Action Property of the *.cs source file to Compile from Content. From the Solution Explorer right click on the source file and choose Property.
Note that the App_Code folder is intended for use in Web Site Projects.
Note that for a Web Application Project or MVC project, adding an App_Code folder to your project and putting *.cs files in it will cause problems. I ignorantly added an App_Code folder to my MVC project from the Solution Explorer. VS defaulted the name space to MyProjectName.App_Code. In this case Visual Studio 2012 defaulted the Build Action to Content, even though the type was .cs code. After I changed Build Action Property of the *.cs source file to Compile from Content the namespace was resolved in other folder locations of the project. However because of problems, I had to change the name of folder--see below.
Important
In the MVC or Web Application project, the App_Code folder is trouble because it has Web Site Project type semantics. This folder is compiled when published (deployed) to the server. By changing Build Action from Content to Compile, you resolve the namespace issue on your development environment by forcing immediate compilation, but you get trouble when the second compilation results in objects defined twice errors on deployment. Put the code files in a folder with a different name. If you converted a Web Site to a Web Application, see the guidelines on the Net for this--not in the scope of this question. To read more about App_Code folder in the different project types see this blog
You need to add
using EMCWebAdmin.App_Code;
to all the pages you want to be able to use the class.
Alternatively you change the namesspace that the class is in to the same one that all the web pages use which presuming it is EMCWebAdmin
then in your class change
namespace EMCWebAdmin.App_Code
{
...
to
namespace EMCWebAdmin
{
...
This is a feature of visual studio, if you create a class in a folder structure, it uses a namespace that follows the folder structure.
If you convert it to a web app it should work. The downside is that it will no longer autobuild your code in app_code folder every time you change it and spin up the app. I have never seen a professional developer use a website project. I have no idea who MS were targeting when they created them.
Yes, put the calsses to another folder(not asp.net special folder), and only use the main namespace for the application is solve this issue.
Thanks johnmcp.

App_Web_XXXXXXXX.X.cs type or namespace not found

I have a web application that is giving me type or namespace not found errors. The bizarre thing is that the errors are happening in an auto-generated file App_Web_XXXXXXXX.X.cs. How can I figure out which file is auto-generating this file so I can fix this issue?
I hit this today using VS2013 and MVC4.
In my case I had added a new Model class, referenced it in a couple of places (a view and partial view) and then excluded the class and the referencing files from the solution / project. When everything was excluded I could not get the project to compile. I added the class and referencing files back in (e.g. "included" them) and then it built just fine.
I subsequently excluded the files that referenced the new class and it rebuilt successfully.
I then excluded the class and it failed again.
Then I included the class and commented all its code out and the compiler returned errors from the referencing files, which were excluded. Very strange. So apparently excluding files from the solution doesn't prevent them from being picked up during builds.
What ended up working for me was to simply make a backup of the files and delete them rather than excluding them.
HTH,
Andy

Type or namespace name does not exist in the namespace - yet the namespaces do exist

A project that I've been working on for a long time without any problems suddenly started throwing errors such as
The type or namespace name 'xxx' does not exist in the namespace 'yyy' (are you missing an assembly reference)?
In this case, the namespaces were all core .Net libraries such as System.Data, Linq, and IO that I've been using without any problems.
I've worked through all of the issues in this question such as making sure all the projects in the workspace are using the same version of .Net for the Target Framework setting.
I removed all the libraries in question and re-added them but the problem persisted. I doubt they libraries themselves are corrupted as I reference them in other projects within the solution.
After rolling back through my work with TFS I managed to work out what I'd done that had created this error: I'd added a folder called "System" to my project and put a class file in it.
This is an easy issue to reproduce: create a project, add a folder called system to it (it will still compile at this point) and then create a .cs file in it, that's when all the fun errors will occur.
The problem stems from the name of the "System" folder which leads to any files created in it being under the namespace ".System".
I can understand why having a folder / namespace called "System" would cause problems now but I think it would help if Visual Studio warned when creating a folder / namespace of such a name to stop this problem happening in the first place. I've logged a bug with MS, at least having this logged might help any other people who have made the same mistake as me!
I had the same issue after my system shut down unexpectedly and even though VS tried to restore, the problem popped up.
I had two projects in my Solution Explorer. To solve this, I right-clicked the project that was associated with the error message and selected 'Build'.
After that, the issue was resolved.
Just to add that the problem was permanently solved after I added a reference to the 'project' from the other 'project'.
I renamed the namespace of the file I was referencing from myproject.shared.constants to myproject.SOMETHING.shared.constants
After that It suggested "use myproject.SOMETHING.shared.constants" as suggestion. I renamed it back to myproject.shared.constants and then it worked.
Try to Clean and Then Rebuild the dll file .I was experiencing the same problem tried different suggestions from internet but none of them work.But it will. In case you don't know how follow these steps:
open your dll project file.
2.click on solution Explorer Right click on Your Dll Project Name you will find Clean and Rebuild Option.

System.Windows.Forms.DataVisualization Namespace Fine in One Class but Not in Another

I'm getting this error
The type or namespace name 'DataVisualization' does not exist in the namespace 'System.Windows.Forms' (are you missing an assembly reference?)
Here is my using section of the class:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms.DataVisualization.Charting;
using System.Windows.Forms.DataVisualization.Charting.Borders3D;
using System.Windows.Forms.DataVisualization.Charting.ChartTypes;
using System.Windows.Forms.DataVisualization.Charting.Data;
using System.Windows.Forms.DataVisualization.Charting.Formulas;
using System.Windows.Forms.DataVisualization.Charting.Utilities;
namespace myNamespace {
public class myClass {
// Usual class stuff
}
}
The thing is that I am using the same DataVisualization includes in another class. The only thing that I can think that is different is that the classes that are giving this missing namespace error are Solution Items rather than specific to a project. The projects reference them by link. Anyone have thoughts on what the problem is? I've installed the chart component, .Net 3.5 SP1, and the Chart Add-in for Visual Studio 2008.
UPDATE: I moved the items from Solution Items to be regular members of my project and I'm still seeing the same behavior.
UPDATE 2: Removing the items from the Solution Items and placing them under my project worked. Another project was still referencing the files which is why I didn't think it worked previously. I'm still curious, though, why I couldn't use the namespace when the classes were Solution Items but moving them underneath a project (with no modifications, mind you) instantly made them recognizable. :\
You are very likely missing a reference to the DataVisualization DLL. Note that although they share the namespace of System.Windows.Forms.dll, they aren't actually contained within it.
Solution items aren't used by compiled assemblies.
http://msdn.microsoft.com/en-us/library/1ee8zw5t.aspx
"They can be referenced by projects, but are never included in solution or project builds"
As far as I know, solution folders/items are really just meant for organizing things.
Are you getting actual build errors or just squiggles? Try building and look at the output window, does it succeed or fail?
In VS 2008 SP1 C# introduced a top level error squiggling feature. It's possible that if you open the solution item version of the file it will squiggle because of a lack of default references. The solution should still build correctly though.
If this is not the case try adding the file directly to the project (no link). See if that eliminates the error. If so then we know it has to due with a linked file and it can help track down the problem.

Categories

Resources