Can't create valid reference to class library - c#

IMPORTANT! I was confusing myself, being in another project/assembly than I expected. Things work ok, when I'm in the right place. Thanks for all help anyway, spend no more time on this one!
I have an MVC3 project, which uses an ClassLibrary-assembly. I would like to create instances from types in that class library from a console application (in the same solution).
I have made sure that I use the fully qualified name, since it's a different assembly than the console app. In my case, the type name is:
KIBS.DykrapportDomain.Model.DykRapport_Utrustning, DykrapportDomain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
I've added a reference to the class library project from the console project, it seems fine when I open it in the Object Browser, I can see everything inside. I have also made sure that the target framework is .NET 4 (not client profile) for either of the projects.
Still, when I add the following statement:
using KIBS.DykrapportDomain.Model;
I get an The type or namespace name 'KIBS' could not be found (are you missing a using directive or an assembly reference?)
What am I missing!?

Check your console application and make sure it targets the full .Net framework and not the the client Profile version.

Check the name space of the class which you want to create the inside the console application and if KIBS.DykrapportDomain.Model then it should work other wise use the namespace that is inside the class

Related

Adding a reference + using directive doesn't 'load' the assembly

I added a reference to a different solution assembly to my project.
I also added a using directive
using MyAssembly;
MyAssembly only exposes one type MyClass.
However when I want to reference it in code I still have to write full name:
MyAssembly.MyClass
Isn't the using statement suppose to solve that? The reason is, I'm getting expections that the MyClass type cannot be found.
Seems like you're having same class myClass on both solutions. Hence, you will need to explicitly provide the assembly name in order to let the compiler know which class of both you are referring to.
Its also possible when you reference higher .Net framework version build dll to lower .Net framework Projects. This is very common issue. But agreed that you face the same issue when you have same class defined in two dlls.

Problems Adding Custom Class Library (dll) to Project

I've created a class library in C# Visual Express 2010. I built the solution making sure that the target framework was the .NET Framework 4 and not .NET Framework 4 Client Profile.
When I try to create a Console Application and reference the library, none of my classes are recognized.
I have made sure the Console Application is also using the .NET Framework 4 and not .NET Framework 4 Client Profile
I have made sure that I am actually using the namespace of my library.
All the classes I want to access are public.
If I change my Class Library project to a Console Application project, everything works.
Restarting and/or reloading the library does not work.
After I reference my library, it appears under References, but I cannot view it in the object viewer. I'm not sure what this means
The only error messages I receive are:
Error 2 The type or namespace name could not be found (are you missing a using directive or an assembly reference?)
I'm not sure why nothing is recognized, or why I cannot use my library. Does anyone have any ideas of where I can go to look for the solution, or has anyone run into this problem themselves?
I know you have tried a lot of things here, but ultimately the following should work
In your solution make sure you have your class library and your console app (Although you don't need to have the class library in the solution, this is just for sanity sake)
Make sure they are the same framework
Make sure they both compile, take note of any errors or warnings
Add a reference to your library (remove any previous references),i.e click your console app, click Add\Reference, click solution, then check your library, click add
Add the using statement to your console app
Just to make sure everything is good , do a Clean, and rebuild
if you have done all the above and its still not working, then this seems like a benign error
I.e if everything compiles, and there are no warnings, there is really no normal reason why this shouldn't just work
All of classes that defines in class library must have -public- access modifier, unless these assemblies don't work!

The type or namespace name 'X' could not be found

I have a CustomerOrderContext class in CustomerOrder.Data project in my solution. In another project, CustomerOrder.App, I have CustomerVM class. In this class I'm creating a CustomerOrderContext instance. But I get this following error:
Error 2 The type or namespace name 'CustomerOrderContext' could not
be found (are you missing a using directive or an assembly
reference?)
I have changed Build Actions of both projects to Page and then back to None and I have started to get this error. I have this line in CustomerVM:
using CustomerOrder.Data;
so I guess I shouldn't get this error. CustomerOrder.Data is also in References list of CustomerOrder.App. Can you tell me how I can fix this problem?
Thanks.
The issue is solved when I created new project, copied & pasted all classes.
be sure that CurstomerOrderContext class is marked as Public and not Internal.
Can you check if the framework versions are compatible? See https://stackoverflow.com/a/4286625/2478357.
Also, if you use Entity Framework (and CustomerOrder.Data uses EF), check if your CustomerOrder.App has a reference to the Entity framework dll. (This also applies to other imported dll's..)
you need to add Reference of that project (.dll) in your project.
You will find it when you right click your solution and add the (.dll) of other project in debug folder.
To access the class you must have same namespace
and
Check that both projects follow the same .Net Framework

I Can't access my class library in a web application

I have added Class Library in my web Project through Add reference option, but i am facing an error
Error 2 The type or namespace name 'UserDetailsDll' could not be
found (are you missing a using directive or an assembly reference?)*
I hope some one will help me.
Thanks in Advance.
There are a few possibilities as to why that is happening:
You're missing a using statement. Try using the whole name of the class (namespace, then class name) you're trying to use.
Your DLL is targeting a different processor architecture. Check the properties of your web application and your DLL and see if they are both targeting the same CPU.
Your DLL is for a different .NET framework than your web application. Check the target framework in the project properties.
Finally, check the references in Visual Studio for a little yellow warning icon next to your referenced DLL. It's presence indicates that the DLL couldn't be found / referenced in the first place.
You need to either add a using in your code file or specify the full name to the class you want to use.
Both solutions can be autogenerated. Rightclick on the class that won't get resolved and click on Resolve -> using XXXX

Building a re-usable class library in C#

I have a class library I've built for a coworker to use that, after giving her the .dll file, I cannot get to work. The name of the .dll file is "BatchDashboard." The name of the namespace is "BatchDashboard," and the name of the class (there is only one) is "BatchDashboard." Is it a problem to have all three similarly named? Visual Studio is able to add the reference just fine. However, this statement:
using BatchDashboard;
spits out the following error:
The type or namespace name 'BatchDashboard' could not be found (are you missing a using directive or an assembly reference?
Likewise, I cannot instantiate a new 'BatchDashboard' object.
Could someone tell me what I am doing wrong?
Thx!
EDIT
I have tried adding the reference to another test project on my computer and receive the same results.
SECOND EDIT
Changing the access modifier to public for the "BatchDashboard" class fixed the issue with the using statement. However, when I try to instantiate an object like so:
BatchDashboard batch = new BatchDashboard();
I got the following error:
'BatchDashboard' is a namespace but is used like a 'type'
It was necessary for me to have different class and namespace names to work. Thank you!
I think, I know what problem you have. When you created your class file, you probably didn't change access to your class to "public". In .Net, unless you have public members in the assembly, the namespace wouldn't be found in "using" directive.
Basically, check if the class you have is a "public class"
Here are my best guesses:
You compiled it against the wrong CPU target
You have a dependency on a Runtime library that should be added as a reference to the
project also.
Make sure that:
The library is added as reference to the project
That library is in a equal or prior .NET version compared to your project's .NET version.

Categories