I have one web application with two projects:
Project "Website"
Using CMS;
namespace Web
{
}
Project "CMS"
namespace CMS
{
public class Functions
{
}
}
Then I want to be able to use CMS.Functions.MyMethod() inside Website.Web.
Im having some problem with this.. Inside the "Website" project I have added "CMS" as a reference and I have also added Using CMS; and even tho the intellisense picks up CMS.Functions I get an error! The word CMS gets underlined blue and I get the message:
The name 'CMS' does not exist in the current context
What am I missing out? Its so weird becuase I can write CMS.Functions and the "Functions" part comes up in the intellisense but when I finish the line the word CMS gets underlined blue and I get the error even tho I got a reference and a Using statement.
From the sound of it, you want to make your Functions class methods to be static
namespace CMS
{
public class Functions
{
public static void MyMethod(){
//do stuff
}
}
}
The most likely cause is that you have not added a reference to the CMS project to your main project. That is the only time that I get the exception about the name not existing in the current context.
Related
Okay this is driving me crazy. I got one almost finished project (which works perfectly) and I wanted to make another one in the same way. The thing is there is a solution with two layers DataAccessLayer and BusinessLogicLayer. Both of these layers have a Model library with all models in the project. I need to convert the model from the first layer to a model of a second layer in the manager library. This works in the finished project I received but I can not manage to make it on mine.
The thing is I can't make the necessary references to work like they do on the finished project. The structure is:
BusinessLogicLayer
--Managers
----Users
--Models
----User
DataAccessLayer
--Models
----User
In the Managers project I have a reference added to DataAccessLayer.Models.
And inside the Users class I got:
using Library.BusinessLogicLayer.Models;
Now in my project this line is red underlined:
Error CS0234 The type or namespace name 'Models' does not exist in the
namespace 'Library.BusinessLogicLayer' (are you missing an assembly
reference?)
I am not even sure how and why this works on that original project. But I can't figure it out so it's working right on my project and the structure is the exact same. Anyone have an idea about this?
EDIT:
Dunno why I didn't upload this earlier. Here is the structure.
https://i.imgur.com/srnySFJ.jpg
EDIT2:
Since it is not quite understandable I uploaded the whole project on github so you can take a closer look at it.
https://github.com/Morsusy2k/Library
And here is the problem:
https://i.imgur.com/DvCvnMA.jpg
From what you described above and from my understanding, it seems that Managers and Models are two different projects. If that is the case, make sure that you add a reference to BusinessLogicLayer.Models in your BusinessLogicLayer.Managers.
If, on the other hand, you have only two projects BusinessLogicLayer and DataAccessLayer then it could very well mean that Library.BusinessLogicLayer.Models is not the name of the namespace.
UPDATE
From the picture that you added, you might need to add a reference to Library.BusinessLogicLayer.Models.Models. You have a folder named Models and a project named Models. Visual Studio automatically generates namespaces based on the Solution name, Solution folders, project name, folders within project.
There were three issues with your code. The first one is that you are supposed to add a reference to Library.DataAccessLayer.Models and not to Library.BusinessLogicLayer.Models. This is due to the fact that you have User in DataAccessLayer.Models and User2 in BusinessLogicLayer.Models.
The other two issues were with the Map method where you are sending incorrect number of arguments to the constructor (you are missing UserId) and the other issues is with your DateOfBirth and DateJoined being in the wrong order in the same method.
using System;
using System.Collections.Generic;
using System.Linq;
using global::Library.BusinessLogicLayer.Models;
using Library.BusinessLogicLayer.Managers.Properties;
using Library.DataAccessLayer.Models; // <-- Add reference to this
namespace Library.BusinessLogicLayer.Managers
{
public class Users2
{
public IEnumerable<User> GetAll()
{
using(DataAccessLayer.DBAccess.Library library = new DataAccessLayer.DBAccess.Library(Settings.Default.LibraryDbConnection))
{
return library.Users.GetAll().Select(user => Map(user));
}
}
private User Map(DataAccessLayer.Models.User dbUser)
{
if (dbUser == null)
return null;
// TODO: Constructor is missing a paremeter. I'll add a temporary one
int tempUserId = 0;
User user = new User(tempUserId, dbUser.Name, dbUser.UserName, dbUser.Password, dbUser.Email, dbUser.DateJoined, dbUser.DateOfBirth) // <-- The last two params are in the wrong order
{
Id = dbUser.Id
};
return user;
}
private Library.DataAccessLayer.Models.User Map(User2 user)
{
if (user == null)
throw new ArgumentNullException("user","Valid user is mandatory!");
return new DataAccessLayer.Models.User(user.Id,user.Name, user.UserName, user.Password, user.Email, user.DateJoined, user.DateOfBirth);
}
}
}
Also, regarding the last screenshot that you provided, you do not have Library.BusinessLogicLayer.Models2 namespace. Remove number 2 to get it to work.
As I don't have permission to update your repo with the fixed code, you'll have to fix it manually based on my answer. Otherwise, let me know so that we see how I can push the code back.
There are a few things you could try:
This error could be appearing within BusinessLogicLayer because DataAccessLayer failed to build. Try building DataAccessLayer by itself to see if you get a different error.
The references might be "added", but you might not be referencing the correct DLL or version for some reason. Check your .csproj files manually to ensure all references and versions are correct, have the right hint paths, etc. If you have any config files, you should also review them to ensure there are no version conflicts.
In addition to checking the references, it is possible to add if-then and switch case logic inside of the .csproj files. This is an MSBuild feature that Visual Studio doesn't support through its GUI, so you may need to copy/update this logic manually in your current .csproj files if any existed.
Check your default namespaces in project properties to see if they are the same as your old project. If you added new files since you moved to this project, they may have been inadvertently added with the wrong namespace and that namespace may be causing a conflict. You could also try using global::Library.BusinessLogicLayer.Models; to see if that fixes or changes the error message or at least if Intellisense is picking the namespace up.
If that doesn't work, review all of your namespaces in all .cs files to see if you have any that have gone rogue.
Since your Models namespace has the problem and you have 2 of them with the same name, try temporarily renaming one of them (yes, every .cs file in one of the projects) to Models2 to see if it provides a clue (such as the error going away or changing).
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.
I have a C# WebApp that we are doing for a client. I have two classes in the project, defined (in separate files) as such...
A general utility library:
namespace Client.WebApp {
public static class General {
public static MyDbClass GetDB() {
//<creates and returns MyDbClass here>
}
}
}
A PageBase class:
namespace Client.WebApp {
public class PageBase : System.Web.UI.Page {
protected MyDbClass dbo { get; set; }
public PageBase() {
dbo = General.GetDB();
}
}
}
When I try to compile, I get the error from the dbo = General.GetDB(); line:
The name 'General' does not exist in the current context Client.WebApp.
I have double-checked that the namespace is spelled right in both files.
I have tried adding the top-level namespace Client.WebApp as a using directive in the pagebase file.
I have tried fully qualifying the name as in dbo = Client.WebApp.General.GetDB();
Nothing has helped and it insists that my General class does not exist.
Funny thing is that while the build hits this error and stops, the code-editor does not see this error, and the statement does not get red-under-squiggled.
The project is targeting Framework v.4.5.2, and I am working in VS 2015.
It is a strange error, in my VS2015 if I set a file Build Action to anything other than "Compile", I get an error underline on any type for that file.
Anyway the solution here is to verify that the Build Action is set to "Compile", I'm not sure why adding a new file would have set the build action to anything other than "Compile". I've also tested trying to add new files in multiple ways (select a text file template and just name it something.cs), it still sets it as "Compile". You should verify that your VS2015 instance is updated with the latest updates.
I had the same problem: "type or namespace could not be found". It turned out that class was in a file with the "Build Action" property set to "None", meaning the file was not being compiled. I set it to "C# Compiler", that solved it.
This happens to me sometimes. Closing visual studio and opening it again always fixes it for me.
I think I might have figure it out that if we create a file in a folder which suppose to have content files it set that file's build type content automatically. In my Case I create a file under app_code folder and it is a class file but the build type is set to Content which make it unavailable for any other classes i have.
May be it has any other reasons too.
Try to create a new Class in the Project folder and then move it to the folder, you want it to be.
VS2012 for desktop .net framework 4.5 normal windows forms applications, not WPF
Hello, I tried to search for an answer, but I'm not sure of the correct terminology. I've managed to break my code, and can't understand what I've done wrong. (i didn't think i had changed anything, but ...)
I have a solution which contains 2 projects. The first project is an executable program, and the second is a DLL, which is loaded at run time and used by the first project.
the first project contains a form, and a static class with public static strings in the same namespace. (and some other unconnected classes). specifically:
namespace project1_namespace
{
static class settings
{
public static string some_words = "some words in a string";
}
class dll_callback{
//.. some public methods here
}
dll_callback dllcallback; // instance is initialised in the code (not shown)
Form form;
public partial class frm_splash : Form
{
private void frm_splash_FormClosing(object sender, FormClosingEventArgs e)
{
// this function actually loads the DLL, ensuring its the last step
//... some error checking code removed for brevity
Assembly assembly = Assembly.LoadFrom("c:\dllpath\project2.dll");
Type type_init = assembly.GetType("project2_class");
object init = Activator.CreateInstance(type_init, form, dllcallback);
//... some error checking code removed for brevity
}// end method
}// end form class
}// end namespace
when the form is closing, the method shown above is called which calls the second projects class project2_class constructor.
in project 2, the DLL, there is:
namespace project2_namespace
{
// how did i get this working to reference "settings" class from project 1??
public class project2_class
{
public project2_class(project2_namespace.Form1 form_ref, object callback)
{
settings.some_words = "the words have changed";
//... some more stuff
}
}
}
Now, i was experimenting with some code in an entirely different part of project2, and VS2012 suddenly started refusing to compile stating:
error CS0103: The name 'settings' does not exist in the current context
the standard solution to this appears to be to add a reference to project2, but that would create circular dependencies because project 1 calls 2 as a DLL.
I really honestly don't think i had changed anything relevant to this, but also clearly I have.
looking at it, i cant see how project 2 would have access to a class in project 1 without a reference, but the list of arguments to the project2_class constructor doesn't include one, and I am absolutely positive that it hasn't changed (and I cant change it for backwards compatibility reasons).
would really appreciate help with this, as its been a lot of work to get this working.
as a side note, I've definitely learned my lesson about not using source control. and not making "how this works" comments instead of "what this does" comments.
may dynamic help you? You can not get the setting string at complie time.
I'm writing a game where I want to use ContentTypeReader. While loading my model like this:
terrain = Content.Load<Model>("Text/terrain");
I get following error:
Error loading "Text\terrain". Cannot find ContentTypeReader
AdventureGame.World.HeightMapInfoReader,AdventureGame,Version=1.0.0.0,Culture=neutral.
I've read that this kind of error can be caused by space's in assembly name so i've already removed them all but exception still occurs.
This is my content class:
[ContentTypeWriter]
public class HeightMapInfoWriter : ContentTypeWriter<HeightmapInfo>
{
protected override void Write(ContentWriter output, HeightmapInfo value)
{
output.Write(value.getTerrainScale);
output.Write(value.getHeight.GetLength(0));
output.Write(value.getHeight.GetLength(1));
foreach (float height in value.getHeight)
{
output.Write(height);
}
}
public override string GetRuntimeType(TargetPlatform targetPlatform)
{
return
"AdventureGame.World.Heightmap,AdventureGame,Version=1.0.0.0,Culture=neutral";
}
public override string GetRuntimeReader(TargetPlatform targetPlatform)
{
return
"AdventureGame.World.HeightMapInfoReader,AdventureGame,Version=1.0.0.0,Culture=neutral";
}
}
Does anyone meed that kind of error before?
I have been encountering the same problem for a week, and finally decided to do a quick check on the whole "assembly" part.
I found a fix!
Essentially, when you go into AssemblyInfo.cs, you will see all the properties(ie Title, Description, etc.)
The Title, sadly made by XNA, is NOT what your runtime reader refers to. Its actually getting the initial name of the project, which it uses to track back to your application (exe) file in your project. Try either re-making your project from scratch, making sure to keep your namespace the same as your project and never change it , or give a go at re-naming your exe file, found in the debug/obj folder in your project(i believe). hope I helped!
-Will