Visual Studio equivalent of the Delphi "search path" - c#

I've made a C# class defined in a .cs file, which I'm using in an existing project.
Let's call it the Magic class, defined in Magic.cs.
Now I'm working on a new project, and I'd like to instantiate an instance of that class in my new project.
How can I reference that class in my new project without actually copying the file into the new project's directory?
In Delphi, the answer would be "add the location of the class definition to your search path". This is an embarassingly stupid question, but I can't find a good answer anywhere.
Here is what I've tried:
1 - Project->Properties->Reference Paths->Add the location of my class (references to the class are still unresolved)
2 - Right click on project -> "Add existing item" -> choose the class (creates a new separate copy in my project folder)
3 - Right click on project -> "Add reference" -> see that it is expecting a compiled target like a DLL.
See also
How do you share code between projects/solutions in Visual Studio?
Visual Studio&Source Control: How to have shared code?

You are in a whole new world. There is no equivalent.
If you want to share the class with out copying it in you create a class library and build your class into that library. Then you reference that built library from your project.
You can add a class library to your solution and the reference the project while you are developing. Class libraries act very similarly to the way BPLs do if you ever used those.
Once you have a class library you can then share the library between your solutions. This can be done in 2 ways - by building the library and sharing the binary - or by including the class libraries project in your solution. At first you probably want to go with the latter method till you get the hang of it, but once things get quite large and your shared code settles down it is better to reference the pre-built binary.
There is learning curve here - but at the end of the day you will be better off.
Good luck.

Joseph, Visual Studio does not provide this functionality, so must reference each class manually. Anyway to avoid to create a copy of your class every time which is included in a new project you must use the a option Add existing item selecting add as link.

Sorry but you cant do this, its good to know that Delphi provide such facility but may be Visual Studio doesn't do this because at the end of the day this may lead to a mess of linked files. Visual studio likes to organize related files in the same project or solution.
If you want to have your earlier class available then as mentioned above you have only two options.
1) Simply copy the existing .cs file in your project directory (By Right click on project -> "Add existing item" -> choose the class )
2) Add the Project in your solution (Right click on Solution -> Add -> Add Existing Project... -> Select the project file from File Browser) and then Add the class refrence (Right click on project -> "Add reference" -> In Projects Tab Select your Project). It will automatically make a reference to the .dll.
3) And last option is to compile your class in a .dll and then add the refrence to it in your Project.
Good Luck

You want to reference either a compiled .Net assembly (dll in this case) or another project that is part of the your solution. If you have the source, you add the library project to your solution. Then add the library project as a reference to the project that uses it. When it compiles, it'll copy the dll over to your build directory. Using this method is nice because you'll be able to step through the code in the library when using the debugger.

Related

File type required to add a reference to a C# project in Visual Studio (2015)

Very new to C#/VS and this is vexing me - there are a lot of similar questions on SO, but none seem specific enough to help me!
I'd like to use a class from ProjectA in another class (eg ProjectB). I wish to "maintain" the class in the original project, ProjectA, and simply "use" it in Project B (if that makes sense?!)
From what I have read here and elsewhere, I first need to set up a reference to ProjectA within Project B - but this is the stage I am struggling with.
When I follow what I have read (eg on How to use a Class from one C# project with another C# project ) I get to the stage where I am in the "Add References" dialog, and have selected "Projects".
But VS wants me to reference specific "Component" file types (.dll, .tld, .olb, .ocx, .exe) but I can't seem to find a file that refers to "ProjectA". I browse to the folder where ProjectA sits, but no files of the correct type exist. There are files such as:
ProjectA.csproj, ProjectA.sln, ProjectA.suo
but none of these can be selected in the dialog.
Can anyone provide what I suspect will be a very simple answer? Thanks!
In the dialog box for Add Reference you have the option to reference a project within the solution. This will add all its namespace classes and make them available for your use in the other project. You need to make the two projects part of the same solution to be able to do that.

A project with an output type of class library cannot be started directly (2)

A project with an output type of class library cannot be started
directly. In order to debug this project, add an executable project to
this solution which references the library project. Set the executable
project as the startup project.
While running the program in C# visual studio 2010 I am getting this error. Please tell me how to sort out this error?
Set your startproject in the project explorer by clicking right mouseclick on an executable project and choose "Set as start project"
You created the project as a class library project, not an executable program. Class libraries are compiled and then used by other applications for common functionality.
If you meant to create a class library, you need to create a test application (or, better yet, Unit Tests) to test the functionality. If you meant to create an executable application, you'll need to modify the project.
If you have multiple projects in a single solution, you might also need to make sure you have the correct project set as the "Start Project" rather than one of the class library projects.
what you have done is that you have made a class library with all your code
every project in .Net needs a starting point like Main() function in c# which obsly isnt present in a class libraty type of project
what you can do instead is
1. Make a console project inside the same solution. by right clicking the solution and add new project
2. Add a reference to the class library
3. Access the classes/methods of the class library and then starting the console project instead of class library project
Alternatively,
If you have a console project OR any other project in the solution already
right click the project in the solution explorer and Set as Startup project
Right click on Solution-->Go to properties-->Expand Common properties tab-->(On right side) Select Radio button with option Single startup project-->choose project from drop down list.
Enjoy.
If you want to debug code in the DLL or run it using tests, go to the properties of the project, go to the Debug tab, select "Start external program" and enter the name of the application to use, such as a test runner.
Go to your project properties page, change Output Type to Console Application or Windows Application (depending on what it is).
Make sure that you've got correct project set as startup one (right click -> set as startup project), and that this one is not of type Class Library. If your only project in solution is class library, then you need to create some test executable for it.
The same error type i have found in my one of project suddenly which is running in VS 2012 with MVC 4 only. I have run multiple ways to resolve this error, but i have found filly with small change option in Property window of your respective error giving project.
I have went project property window there i have change startup Project from Healpars to Myproject.Then it is working fine.
Thank you
I was surprised to learn that you can indeed set some Class Libraries as Startup and run them, IF they contain user controls. They run in the UserControl TestContainer which allows one to modify properties, etc. Basically debug at DesignTime, which can be really helpful. I have about 20 user controls in a single Project. This way I can run the TestContainer and choose which user control to run and debug.
If it is a function app project that you want to execute then you need to have Azure development tool installed in your visual studio. Please see the below image for more info.
enter image description here
You need to mark the API in the set as startup project
Right key on the API
select .net core project, then it will start directly with no doubt. Thanks

How to Create a DLL file in Visual Studio C# 2010 Express edition?

I have already come across the Stack Overflow question "Is there a way to generate a DLL file from Visual Studio Express without explicitly creating a DLL project?", but it does not directly answer my query, so I'm raising it here.
The problem I am facing while trying to make the DLL is that I can't find any option under Build named build class file.
I have changed the project property to class file (shown below)
This is how it is:
And here is how my build option is getting displayed:
Also when I am using the command-line option the dll file is getting generated but it is not getting the properties I'm setting in the application.
I am new with Visual Studio so a litte bit confused about this part.
The "Build Solution" option in your second screenshot is the thing you need to click to produce your dll, alternatively you can right click on your project in the Solution Explorer and click "Build":
(If you only have one project in your solution then these two will both do exactly the same thing)
The output dll will normally be placed in the bin\Debug or bin\Release directory depending on whether you are in Release or Debug configuration, check the "Build" tab of the project properties for the exact path.
The reason why you aren't seeing a "Build class file" option is because this is what the "Build project" menu item does - it will produce a class library if the project output type is "Class Library", a windows executable if the project output type is "Windows Application" etc...
You're not trying to build a class file - you're trying to build a class library.
And you just build the solution - that will build each of the projects in your solution, including your LicenseCheckLibrary project is just a class library project.
It looks like you're basically there - look in the bin\Debug or bin\Release folders under LicenseCheckLibrary, and you'll find the DLL.
Why would you want to avoid building a DLL file in the first place? Are you developing an EXE file in order to test the logic and then conver it to DLL once it is working fine? If yes, why not create two projects: Windows Console and Class Library. Inside Class Library implement the licensing logic and use Windows COnsole to test the logic. When you say you are new with Visual Studio, what exactly do you mean? You never used it before or you are new to .NET Framework programming? .NET Framework has certain classes for developing licenses. Also, there were quetions here on stackoverflow regarding the licensing. Find some of them instead of reinventing the wheel.
Have a look at this article http://www.developer.com/net/net/article.php/3074001
Create a new class library project
Create classes and code
compile Project
Dll Created
Create a new project
Click on Add Reference
Navigate to the class library folder
Go into the debug folder or whatever and include
Remember you will prob have to include the namespace. in the new
project.

C# using others code

Iv'e downloaded a C# interval tree collection class class from here http://intervaltree.codeplex.com/SourceControl/list/changesets -> Right hand side -> Download.
However I can't open the whole project on my Microsoft Visual C# 2010 Express (that also runs C# XNA) because
Solution folders are not supported in this version of the application
Also I just want the class to use separately in my own seprate project.
I tried to copy the three important seeming files Interval.cs, IntervalNode.cs and IntervalTree.cs into my project but this generated the compile error
There are no importers which handle this file type
I've also tried to copy and paste the contents of the three files into my project, encapsulating them into there own namespace as well as there was a lot of code. I had to rearange some of the usings a little but have run into the problem that possibly it wants PowerCollections .dll and .pcb files as using Wintellect.PowerCollections; causes
The type or namespace name 'Wintellect' could not be found (are you missing a using directive or an assembly reference?)
I'm not sure how to continue or if I'm doing the right thing at all in how to get this class to work.
Add the library to your solution
Copy the IntervalTreeLib directory into your solution directory. Then, right-click your solution, and add existing project. Point it at IntervalTreeLib.csproj in IntervalTreeLib, and click Open. That should add the IntervalTreeLib project to your solution.
Add a reference to the library in your project
Then, in your project, add a reference to the IntervalTreeLib proejct:
- Right click the References folder, and Add Reference. Click the Projects tab, and select IntervalTreeLib.
Use the classes in your code
To use classes from the library in your source then, you need to either add:
using IntervalTreeLib;
void Foo() {
IntervalTree<int, int> tree = new ...
}
Or, refer to them by their full name:
IntervalTreeLib.IntervalTree<int, int> tree = new ...
Open the IntervalTreeLib.csproj file if you want to be able to open the project in it's entirety (or in your current solution add an existing project (you can right-click on the solution) and select the IntervalTreeLib.csproj). If you are trying to grab just the code file in your project, ensure you also grab the PowerCollections.dll file (I see it is in the same folder as the code files) or your code will not compile (as you have discovered). You'll need to add a reference to it and include the needed using statement at the top of the code files making use of this library (or use fully qualified name with the namespace).
using IntervalTreeLib;
or
var myObj = new IntervalTreeLib.[WhateverClass](...);
Also, make sure you read the license.txt file. You may need to include it if you are using the code. Give credit where it is due.
UPDATE:
If the test project is causing you problems, just open the library project. Ideally you could just open that and compile it, adding the output DLL files that are generated directly into your solution. This is ideal unless you are planning on changing the library source code itself.
Add the library to the references of the project you want to use it.
Since discussing that you are able to build Intervallib.dll, we will discuss about how you should the dll in your project.
Now in your proj, right click on the references part and add the dll intervallib.dll to your references. In your game.cs file, have the reference to the namespace as -- using IntervalTreeLib;
then you should actually copy the dll powercollections.dll to the bin directory of proj directory also.
you should copy this dll because there is an indirect link to the dll as it is used in IntervalTreeLib.dll
following these steps, I was able to execute this project.

Reusing code in .NET website (app_code folder)

I need to use some classes inside the app_code folder of a website project in visual studio 2008. I need to access this code from a class library project in the same solution. I cannot add a reference to the website, and I'm not sure of the easiest way to use the classes that already exist here. Do I need to move it to a class library?
What other options do I have?
Yes, create a class library and move any types you need into that library. This library can be referenced in as many places as you would like.
The best way to do this is to put those classes in their own library.
However, if you really don't want to do that, you could add a link to the files in the library project. To do that, right-click the Class Library project or a folder within it, Add, Existing Item, navigate to the code files, click the down arrow near the Add button, Add As Link. This will add the same file to both projects. You can even use the #if preprocessor directive to limit portions of the file to specific projects.
However, it is vastly preferable to put the code in a library and reference it in the web project.

Categories