Unable to reference one class library from another - c#

I have an assignment about OOP in C# that involves me creating a base class (in the form of a class library) and a derived class (as another class library). I realize that for the two to function properly I need to reference the base class' .dll in the derived class. However, I am unable to generate that .dll file for the base class, as I get the error "A project with an Output type of Class Library cannot be started directly". My textbook says the fix for this is to click "Build Solution" in VS 2013, which should make it runnable. I've done so, and yet mien still does not function. Where am I going wrong?

"A project with an Output type of Class Library cannot be started directly"
This means that you are trying to run a project marked as a library, not just build it. Just compile this project without trying to run it, and create another project that is runnable (e.g. console app, win forms app, etc) and reference your current solution from that new project.
Alternatively, change the type of the current project to one that is runnable.

Related

How to use class from other project in visual studio C#

I have a Xamarin.Forms project and a C# console app project. I want to use one class from the console app in my Xamarin.Forms project.
I added the console app project to the solutions explorer of the Xamarin.Forms project.
Unfortunately, I can't figure out how to use the class from the console app in one of the Xamarin files.
I always get the error message:
The name 'MyClass' does not exist in the current context.
I tried to press alt+enter to show potential fixes but it does not offer me the option of importing/using the class.
I also wrote manual using directives in various forms but it still does not seem to make the class accessible.
The only way I was able to use the class was by adding the class directly to the Xamarin project by adding it with add->existing item. The problem with this is that it imports a copy of the class. Since I'm still working on the class within the other project the added class is fast outdated and I have to manually copy its contents over.
How can I use a class from an external project without making a copy of the file?
Instead of access class from console app(its exe) try creating new reusable library add that class and use in both projects also you can write wrapper class in both projects
Try to add a reference to the second project in your first project. To do this, right-click on your project, select Add Reference then select the project in your solution. Once your main project references the second project, then you can access its public types.

A project can't reference another project of the same solution - the method or operation not implemented

I would like to use some of methods from my c# console app project ("EPS logic") in UWP project ("EPS view") in same solution.
I tried that to add reference- Calling methods from different Projects in one Solution.
But when I tried to I got two errors:
Cannot add reference to project "EPS Logic"
and
The method or operation is not implemented.
Additionaly I got some warnings like this about all classes from my EPS logic project:
warning DV2002: Class 'SequenceMaker' is not mapped to any Dependency Validation diagram
But I don't know if it's part of that problem. Should I not try to add anything to UWP project in first place?
You will not be able to reference a console app from a UWP project; they are not compatible! If it is really just logic, you should create a .NET Standard class library or a UWP class library and put your logic there. You should be able to reference that just fine.
If you are wanting a console app and a UWP app with shared logic, split that logic out into a .NET Standard class library, then reference that from both the console and UWP projects.

How to run C# class library which is added to VB.NET existing solution?

Hi I have console application which is written in VB.NET. For this I added c# class library with existing solution. I wrote code in c# class library. Now when I try to run, it is going to VB.NET console application by default. I am not able to run my c# class library.
For clear understanding lets call VB.NET project as classVB and class library as classC#
I tired these methods to make it work :
1) Added classC# reference to my classVB project and made classVB as startup project. I used using statement also to refer to my classVB project as Using classVB. And I put break point in my classC#.But still it is pointing to classVB project
2) Tried to made classC# as startup project( Even I knew this doesn't gonna work). For this I am getting usual error which says "A project with an output Type of class library cannot be started directly."
3) Right click on solution and start up project option -> Single startup project and selected classVB.
But none of this is working. It is always pointing to my classVB project even after I put breakpoint on c#class library code.
Its the first time I am working on library class, so any help on this would be appreciated.
You can't directly run a class library. It is not executable. You can reference the code from it in your VB project, but a class library can never run by itself. To access a public method in your class library from VB, refer to it by Namespace.ClassName.MethodName.
Change the compilation output of the library to the same path where you added the reference in the VB.NET application. Generate the library and check the build is successful.
Do not forget to change the library class to configuration debug in the solution to generate the PDB file that will allow you debugging. Put a breakpoint and try again.

How do I compile C# code as a library instead of an executable?

I have a C# console application in Visual Studio 2010. It has a Main() method as well as a bunch of utility classes. I'd like those utility classes to be available to other solutions. From reading online it seems that I need to compile it as a Class Library (DLL). So here's what I did:
Went in Visual Studio to "Project > [ProjectName] Properties > Application" and changed "Output type" from "Console Application" to "Class Library"
Rebuilt; ProjectName.dll was created in bin/Debug.
Created a new Console Application
Solution Explorer > Add Reference > browse to ProjectName.DLL, select it.
However, neither IntelliSense nor the Object Browser could find the classes inside that DLL.
I tried recompiling several different Console Applications as Class Libraries and got the same result. I also noticed that it works if I initially create the solution as a Class Library, but not if I convert it to one later.
Any tips?
You do not need to build it as a dll. VS 2010 (and IIRC 2008) allow referencing exe assemblies. All you need is for they relevant types to be declared public - top-level classes defualt to internal if you don't add a specifier.
You can switch output type to Class library in project properties as well - then you will have an output as dll instead exe file
What I've always done (since this is what you do with C++ static libraries, which is what I normally use - though I think it has some advantages for C# too) is add the class library's project to the solution, then add a reference to it in the project (or projects) that uses it.
When you go to add a reference, the list of potential references includes items from the solution, so it should be fairly obvious what to do. You should then get intellisense for your library.
One advantage of doing things this way is that if you need to edit files in the library project, it's very straightforward because they are close to hand, and the project then gets rebuilt automatically when you compile the solution.
Make sure that the classes in your dll project are public.
At first, from the point of view of managed libraries it does not matter what kind of Output type is your managed library. I mean that you can successfully reference ConsoleApplication1.exe from ConsoleApplication2.exe project (so you have no reason to convert ConsoleApplication1.exe to ConsoleApplication1.dll).
At second, I've tried to reproduce your situation, but... without effect. My VS displays types/methods from ConsoleApplication1.dll. One reason I can suppose is that you have forgotten to set visibility modifier (public keyword) for your utility classes.

VS 2008 C# : Class library is not accessible in another class library in same solution

I have a solution in VS 2008 which has one web project and 3 Class libraries as 3 different Projects. One project is for DataAccess and one is for BusinessLogic.
I have a class in DataAccessLayer. From there when I am trying to access the Class of BusinessLogic class library (project) it is not coming in the IntelliSense when I type. I used the same namespace in both projects. Still same results.
Do I need to to create DLLs for the first project and add as reference to second?
You need to add reference to this project in another project in your soultion.
Visual studio has an option to add project as a reference, so you don't have to add assembly files directly
You need to reference the library in the other projects.
To do that, right-click the references folder in the Solution Explorer, click Add Reference, go to the Projects tab, and select the library that you want to reference.
EDIT: Also, make sure that the class you are trying to use is declared as public (eg, public class MyClass).
If you leave out the public modifier (which is the default), the class will only be usable in its project. To expose classes and members to other projects, add the public modifier to their declaration
You'll need to add a reference to the project containing the BusinessLogic class in the DataAccess project. Otherwise, the compiler doesn't have anyway of finding the implementation of your BusinessLogic class, even if it does use the same namespace.
This may sound silly, but have you specified the class in question as Public or Friend? They'll need to be "shared" in that sense in order to be properly picked up and used within the other applications, even when the project reference is specified.

Categories

Resources