Namespace/Reference errors - c#

My solution wont compile. I am getting an error message when i try to compile my project:
Error 2 The type or namespace name 'Security' does not exist in the namespace 'Base' (are you missing an assembly reference?)
It is confusing, however, because i have referenced the project and that is the correct namespace! here's the solution setup
Solution Base
- Base.Domain
- Base.Security
- Base.Tests
- Base.WebUI
in Base.Security I have a custom role provider file like this:
namespace Base.Security.Providers
{
public class EFRoleProvider : System.Web.Security.RoleProvider
{
//code here
}
}
I have referenced Base.Security in Base.Tests and in Base.Tests I have the following file (that is giving me error):
using Base.Security.Providers;
namespace Base.Tests
{
class Program
{
static void Main(string[] args)
{
var a = new EFRoleProvider();
//more stuffs
}
}
}
I don't get it.. why can I not access Base.Security types from Base.Tests?

Make sure your projects are built against the same .NET version. I've had this issue when adding a reference to a .NET 3.5 project from a .NET 4.0 project.
To check/change the .NET version right click on your project, select properties, and under the "Application" tab make sure the "Target Framework" is identical for each project.
Your code looks correct, it's an issue with the way the reference is set up.

Related

Referencing classes in a Razor Class Library

I created a new Razor Class Library (using "dotnet new razorclasslib"). I then created a folder called Models in the new library and it contains a class with the following test code:
using System;
namespace TestLibrary.Models
{
public class TestModel
{
public string TestMethod { get; set; }
}
}
However, when I try to use this class from the web application project (by referring to it as TestLibrary.Models.TestModel) I keep being told that the TestLibrary.Models namespace doesn't exist. The exact error is:
The type or namespace name 'Models' does not exist in the namespace 'TestLibrary' (are you missing an assembly reference?) [TestApplication]
I'm assuming I'm missing something simple, just can't figure out what.
This issue was somehow related to the issue posted here: Razor Class Library MSBuild MSB4062 Error During Compile. Once that was resolved this issue went away.

How to work with a main project and sub-projects in visual studio using namespace

I'm not familiar with namespaces and can't make it to work in visual studio 2017 community.
I created a solution with a empty project, I added a class file in that project:
namespace MainProject
{
class MainClass
{
//some methods
}
}
then I added a empty project to the solution and also added a class file:
namespace MainProject.SubProject
{
class SubClass
{
MainClass var;
public SubClass()
{
var=new MainClass();
}
}
}
The solution structure is like that:
Solution
|
|__MainProject
| |__MainClass.cs
|
|__SubProject
|__SubClass.cs
But I receive the error: error CS0246: The type or namespace name 'MainClass' could not be found
I don't understand what I'm missing.
You seem to have two problems (unless you've glossed over referencing MainProject from SubProject).
1) Classes are internal by default. For it to be seen by another project, it either needs to be intentionally exposed to that other project or, more commonly, marked as public:
public class MainClass
{
//some methods
}
2) You haven't added a reference to your MainProject project from SubProject, so even if you fix #1, you'll still have a problem.
You can add a reference by right-clicking the SubProject in Solution Explorer, selecting References (or Dependencies if you're working with .NET Core/Standard), selecting Add Reference, and then selecting your MainProject from the Projects list.

Namespace not recognised in using statement. "The type or namespace '#' does not exist in the namespace '#'. Are you missing an assembly reference?

I have a solution with about 14 projects, and am having a problem with a link I am trying to make between two of the projects:
Argus.Web (a web application project)
Argus.Domain.Office.Command (a project that reads data from an API and stores data in a local SQL Server database)
(there is also a project called Argus.Domain which I mention as it may possibly be part of the problem, although it hasn't caused problems in other similar places in my code.)
Argus.Domain.Office.Command has a context (argusOfficeContext) linking it to a database which I would like to access from Argus.Web. (This context works well in other situations.) When I try to add a using directive to reference the context's namespace in a class within Argus.Web I get the error in the title.
Here is the class I am scaffolding up (in Argus.Web) where the error occurs:
using Argus.Domain.Office.Command.Models;
namespace Argus.Web
{
public partial class Calculator
{
}
}
The red wiggly line appears only under 'Office' in the using, and the error in full is "The type or namespace 'Office' does not exist in the namespace 'Argus.Domain'. Are you missing an assembly reference?".
I have checked that the project Argus.Domain.Office.Command is referenced from Argus.Web and when I check Argus.Web build dependencies I see Argus.Domain.Office.Command (as well as the other project Argus.Domain), and it is set to build before Argus.Web. When I check in Argus.Web References > Projects the project Argus.Domain.Office.Command has a tick next to it and I conclude that it is therefore referenced.
Following Peter D's comment I checked the namespace declaration, and it is declared in the project Argus.Domain.Office.Command.
(Probably irrelevant:) here is the class I am trying to reference
using System.Data.Entity;
using Argus.Domain.Office.Command.Models.Mapping;
namespace Argus.Domain.Office.Command.Models
{
public partial class argusOfficeContext : DbContext
{
static argusOfficeContext()
{
Database.SetInitializer<argusOfficeContext>(null);
}
public argusOfficeContext()
: base("Name=argusOfficeContext")
{
}
public DbSet<Location> Locations { get; set; }
public DbSet<LocationPeriodReport> LocationPeriodReports { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Configurations.Add(new LocationMap());
modelBuilder.Configurations.Add(new LocationPeriodReportMap());
}
}
}
Both projects target .NET Framework 4.5.1, and I have looked for the answer in various places, but the fixes for the other questions below don't seem to fix my case. I'm running Visual Studio 2015. Other questions for reference:
'Using' not detecting namespace-checked client profiling already
Visual Studio 2010 suddenly can't see namespace?
Visual Studio 2015 C# Not finding references
If anyone can advise what I'm doing wrong I would really appreciate it. I have checked lots of other posts looking for an answer, but if you can provide a link to an answer I have missed that would also be great.
Most of the times I see this, the cause is a class with the same name as part of the namespace. These are in two different assemblies.
namespace Library.Foo // assembly 1
{
using Library.Foo.Bar; // error here
public class Bar
{
public void DoIt()
{
new Bell(); // and here
}
}
}
namespace Library.Foo.Bar // assembly 2
{
public class Bell
{
}
}
This can be a real pita to find. I suggest doing a test search of each word in your namespace. You're looking for a class of the same name.
As a side (and there's controversy over this), you can avoid some of these collisions by moving the using inside the namesapce.
OK, so I came up with a solution.
I thought I would try removing and recreating the references, and in a nutshell, this worked. Detail:
I right clicked on Argus.Web in the solution explorer, and then Build Dependencies > Project Dependencies. I tried deselecting Argus.Domain.Office.Command, but got the error 'This dependency was added by the project system and cannot be removed'. I'm not sure why this error appears, but I therefore tried right clicking on the references in Argus.Web, and selected 'Add Reference'. In the Projects section of the window that came up I was able to deselect Argus.Domain.Office.Command, so I did this, commented out the problematic using directive, cleaned and rebuilt my solution, added the reference back (right clicking on the references in Argus.Web, and selecting 'Add Reference'), and it all works again.
I guess that the original references must have been misconfigured, but at present I can't see how this came about. I will add to this post if I work out how this happened.
Many thanks to all respondees, all comments and answers were useful.

Unable to find the enum reference which is declared in same namespace in .net 3.5

I have created a class library project which has number of classes and enums. All are declared in same name space. However, while compiling the class library itself some other classes/methods reports error that not fininding the reference to perticular enums. I have declared enum as public.
namespace CommonInterfaces
{
public enum SettingsType
{
EnumType1,
EnumTYpe2,
EnumType3
}
}
namespace CommonInterfaces
{
public interface IPlayer
{
void Add(SettingsType type); <-- Error occured.
}
}
I tried giving full qualified name like CommonInterfaces.SettingsType but still it did not resove the problem. The exact error is
Error 1 The type or namespace name 'SettingsType' does not exist in the namespace 'CommonInterfaces'(are you missing an assembly reference?)
How to resolve the error?
Maybe its nested in other class? Then u have to use it like this: OtherClass.EnumName. Its only idea i have now. Such error could be solved only by knowing ur source code or at least where u define Enum and where it cannot be found.
i ran into this problem while using xamarin in vs2019. i cleaned and rebuilt solution again and again but didnt work then i restarted visual studio and problem gone.
I ran into this problem in VS2019. Restarting Visual Studio solved the problem.

Is there a way to reference a certain class/interface/... by enclosing it with its namespace rather than a using directive "using namespace_name"?

Is there a way to reference a certain class/interface/... by enclosing it with its namespace rather than a using directive "using namespace_name" ?!
As, I'm working on a website, which uses SAP .NET connector. I already added a reference for connector ddl, and while referencing its namespace "using namespace_name", or set class namespace to another one rather than connector namespace,
I got error regarding connector classes with that error message "The type or namespace couldn't be found, are you missing a using directive or an assembly reference?".
But while changing namespace name to connector namespace, everything is going well?!
// Set namespace to be IDestinationConfiguration interface namespace.
// Using this, everything is going well.
namespace SAP.Middleware.Connector
{
public class ConnectorConfiguration : IDestinationConfiguration
{
}
}
// Using that way; it's not working, and got an error regarding IDestinationConfiguration even it belongs to refernced namespace.
using SAP.Middleware.Connector;
public class ConnectorConfiguration : IDestinationConfiguration
{
}
So, connector types forced me to set namespace of class to their namespace!
Is this possible? If so, how?
I ran into this too and couldn't figure it out until I finally found the answer on a post at http://forums.sdn.sap.com/thread.jspa?threadID=1876430, the last line which said:
"Also, make sure your target framework is ".NET v4.0" NOT ".NET v4.0 Client" -- that way you get the System.Web namespace that is required."
My project was targeting 4.0 client target framework. Once I changed it to just .NET Freamework 4.0, all the references worked as expected.
Is this what you are after?
public class ConnectorConfiguration: SAP.Middleware.Connection.IDestinationConfiguration
{
}
You can write all your code without usings if you like, you just need to use the fully qualified namespace name for every class/interface where the using isn't used.
If you try this:
using SAPTEST = SAP.Middleware.Connection;
namespace TestNamespace
{
public class ConnectorConfiguration: SAPTEST.IDestinationConfiguration
{
}
}
If that works, but it doesn't work if you remove SAPTEST, then IDestinationConfiguration must be declared in another namespace too.

Categories

Resources