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.
- 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.