I have a solution that contains two projects developed in visual studio 2012 express, and both targeting the .net framwork 4.5.
the first "Dao" project purpose is to take data from a database. and take these data to the second project as a dll library
the second project "UI" purpose is to display data coming from dll library
when i added reference to the second project and wrote using statement, I got the following error:
The type or namespace name 'Dao' could not be found (a using directive or an assembly reference missing?)
I tried to change the target of the two projects to .net framework 4.0 and .net framework 3.5 , but I got the same error.
I also add this piece of code to be sure that the target is change but I got true :
using System;
using Dao; // error
namespace Ui
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Type.GetType("System.Reflection.ReflectionContext", false) != null);
Console.ReadKey();
}
}
}
What do I need to do to fix the problem? Thanks a bunch.
You need to add a reference to your Dao assembly from your UI assembly. Right Click on References, Add Reference. In the Projects tab, select your Dao project and hit OK.
First things first:
Add a reference to Dao - in source explorer right click references->Add->projects tab.
Add a using statement at the top of your code something like using Dao;
Ensure Dao is a public class
This way your code will know to reference Dao, it is usually better to create a new instance of Dao:
Dao example = new Dao();
Then when calling Dao you would call example instead, so example.(name of method)
Related
Background: I am a novice in C#, and use Visual Studios 2010 Express.
I have a class (let's call it myclass) which I want to use in multiple projects. I used to add classes with Project->Add Existing Item... Which creates a copy of myclass.cs.
Now I just found out that when I build the original myclass.cs it creates a myclass.dll and places it in the release folder of my project.
But when I try to use this DLL, I get the following error:
The type or namespace name 'myclass' could not be found(are you
missing a using directive or an assembly refference?
Which is weird to me, because I already have referenced it (it is also in the Reference folder of my Solution Explorer). And I already have added this to my code:
using myclass;
So what am I doing wrong?
Update: When I tried my old method (add existing item -> myclass.cs) the error message goes away. So it's not a matter of spelling things correctly.
Add the dll first:
Click on references in your project-explorer in visual studio and add your dll then you can use it as you expected it.
Add the reference in your project and check that the target Framework version of that assembly fits the project.
Check the namespaces inside the assembly and then use them like:
using YourAssemblyNamespace.class
Okay so I found the answer myself. It turns out that when you use the using function, it automatically searches for all public classes in the namespace you want to use.
If it can't find a public class, it refuses to recognize the DLL.
Furthermore, not specifying a class makes it internal.
So:
class myclass // internal!
private class myclass // private!
public class myclass // only this makes it visible for others!
Everything was okay after changing class myclass into public class myclass.
In my current personal project, I have a Core project, a DependencyResolution project, and a UI project (WPF application). In the UI project, I've tried to make a call to the DependencyRegistrar class that's in DependencyResolution in both MainWindow.xaml.cs and App.xaml.cs. There seems to be an issue with the UI project referencing the DependencyResolution project, as this compiler error always occurs:
The type or namespace name 'DependencyResolution' does not exist in the namespace
'DMTools.InitiativeTracker' (are you missing an assembly reference?)
D:\Dev\InitiativeTracker\src\UI\App.xaml.cs 2 33 UI
Just including the using statement anywhere in the UI yields that error.
using System.Windows;
using DMTools.InitiativeTracker.DependencyResolution;
namespace DMTools.InitiativeTracker.UI
{
public partial class App : Application
{ }
}
I can reference the Core project (using DMTools.InitiativeTracker.Core) and everything compiles okay. Furthermore, I can make references to DependencyRegistrar in the IntegrationTests project (another Class Library) and it compiles okay.
You can find the complete source here. Feel free to request specific snippets if you need more info.
EDIT:
I have added an assembly reference to DependencyRegistrar both via ReSharper and the typical Visual Studio way. Both cases did not resolve the issue. To the other comment: When I add a reference to a Core static class (IoC), the XAML remains unchanged but it compiles fine.
Tigran's comment figured out the mystery. In the UI project, I changed Target framework from ".NET Framework 4 Client Profile" to ".NET Framework 4" and it all works correctly.
The only thing I can think of as to why this was an issue with DependencyResolution and not Core is that the former makes references to third party utilities (i.e. Ninject) which has issues with Client Profile, but I'm only guessing here.
I have a solution in Visual Studio 2010 containing 6 projects (1 web application, 4 c# class libraries, 1 c# console application).
The console application is my test harness and use this to test external web services, output from methods from within my other libraries and general experimentation. This test console application has only one dependency on another project dependency, one of the C# libraries.
The referenced C# library is pretty simple:
namespace GowallaAPI
{
public class Gowalla
{
private static readonly ILog log = LogManager.GetLogger(typeof(Gowalla));
public SpotsInRadius GetGowallaSpotsInRadius(decimal lat, decimal lon, int radius) {
//snip
}
//other methods removed for brevity//
}
}
I have added to my console application a project reference:
And I've also right-clicked on References and selected Add Reference...
Then, I've gone to my console application and added;
using Gowalla;
Then hit build. I get this:
The type or namespace name 'Gowalla'
could not be found (are you missing a
using directive or an assembly
reference?)
I am completely baffled. I have:
Remove the dependencies completely (and then rebuilt with Gowalla references removed), and added them again.
I have removed the dependencies completely (like #1) and then added them as assemblies only (Add Reference...).
Checked that the target framework for both console application and class library is .NET 4.0 - they are.
Checked that all necessary items within the Gowalla class library are marked as Compile in the Build property.
Jiggled the build order of the project so that I am at least building the console application AFTER the library is built.
Done some shouting and swearing.
Given up and then returned.
Moved the Gowalla C# library out to its own project entirely and then referenced the assembly (like in 2).
Playing the having a constructor in Gowalla and not:
public Gowalla()
{
}
... and nothing has worked!
Can anyone see something obvious? Am I being utterly stupid? I have been on this for hours and I wonder quietly if this is a classic 'wood for the trees' moment...
Help appreciated.
EDIT 1: This is the Gowalla.dll exposed from Reflector:
ANSWER: After #gov's helpful suggestion to remove the GowallaAPI library and try and add something else I did that and started adding in the old code from the GowallaAPI library. Everything worked until I added:
private static readonly ILog log = LogManager.GetLogger(typeof(Gowalla));
log4net for some utterly bizarre reason kept throwing the build. Alas, after removing the line (the reference to log4net remains), the project built and worked perfectly thereafter. Thank you to #gov for setting me on the right path! :D
I had the exact same problem with log4net and it was resolved after changing target framework of the hosting project from ".NET Framework 4.0 Client Profile" to ".NET Framework 4.0"
I suggested him various things in the comments looks like one of them worked out.
#dooburt just forget about GowallaAPI and create a separate project like i say , sample.common and have a public class called utilities or so add that project here , just check a new project of type library and see whats the problem
Take a look at the .csproj XML, see if there is anything odd about the reference, one of these:
<Reference Include="Gowalla" ... />
<ProjectReference Include=".\path to\Gowalla.csproj" ... />
Have a look at the target framework of your class library and the test harness. I was having this error when the class library was set to .Net Framework 4 and the test harness was .Net Framework 4 Client Profile.
I have the following code:
using System.Configuration;
namespace test
{
public partial class MyService : ServiceBase
{
public static ReadConnectionStrings()
{
ConnectionStringSettingsCollection connections =
ConfigurationManager.ConnectionStrings;
However, it doesn’t recognise ConfigurationManager. I took this code directly from here
So I’m clearly missing something, but can’t identify what.
Do you have a reference to System.Configuration? It's not added to .NET projects by default.
I was having the same issue.
It took me a little while to figure out that adding the reference is not adding the using. I had to right-click the project and select Add Reference, then pick System.Configuration in the .NET tab.
Worked like a charm!
Ensure that the actual project you're working in has a reference to System.Configuration. I was working in a data access project, not the presentation layer's project. So I was getting a bit confused because I had thought I had a reference, but in reality the data project was missing the reference.
I had to download the assembly refernce then add to project not sure if others had this problem
Is it possible to link C# and Visual C++ projects under 1 solution?
How can I do this?
I hav created a class in c# which contains two methods display() and ListSql().
And how to use these methods of c# project in vc++ project.which type of project i need to
create to do this.
I do like following
I have created a classlibrary which contains a Test class in that Display() and ListSql()
and i have a refence to c# project from vc++ project.
and i have writen as. i didnt write any import statement
using namespace Test;
void main()
{
}
// error namespace Test doesnot exist like wise error
If you are asking whether you can have two projects with different languages in the same solution, then yes. Just right click on the solution and add the project.
From that point you would need to add a reference from one project to the other.
Project -> Add Reference