4

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

Community
  • 1
  • 1
JosephStyons
  • 57,317
  • 63
  • 160
  • 234

4 Answers4

7

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.

Neil
  • 1,605
  • 10
  • 15
  • Oh for crying out loud. Talk about making simple things difficult. That's amazingly obtuse. I mean, er... that's awesome and really makes my day. Thanks for the great answer. – JosephStyons Jan 20 '11 at 04:02
  • @JosephStyons - Hey I have been through where you are right now and in the long run you will find it is a better world. Perhaps that is the cool-aid talking. – Neil Jan 20 '11 at 04:06
  • 3
    @Joseph, that's really the way you should manage your Delphi code, too. Allow each unit to belong to exactly one project, and never let Delphi compile a unit that doesn't belong to the current project. It helps prevent you from writing project-specific code in a unit that doesn't belong to the project. If two projects need to share some code, make that sharing explicit by putting it in a library. This also prevents you from accidentally sharing code you didn't intend to share. (Adding the *Magic.pas* folder to your search path means *all* files in that directory are accessible!) – Rob Kennedy Jan 20 '11 at 04:15
  • @Rob: Point taken, thanks for the advice. It does seem like overkill in my current (small) project though. FWIW, in the time we've all had this exchange, I've created a couple of class libraries, and pointed both projects to them, and it was not really that hard. I still feel a desire to whine though. – JosephStyons Jan 20 '11 at 04:34
  • 2
    @JosephStyons - frequently I found my self bending over backwards to find the way to make C# behave just like Delphi did - mostly because I thought in terms of how I would solve a problem in Delphi (virtual static members, unit initializers, using the finalizer like a destructor etc). Every time I succeeded I ended up regretting it in the long run. There is a lot of similarities between C# and Delphi, but they each have their own way. – Neil Jan 20 '11 at 04:52
  • You can't really use a class library in Delphi. If anyone adds a private member variable to a class, or reorders class methods: every application that uses that BPL will have to be recompiled or else it will get access violations. Plus i don't want to have to ship a DLL with my exe. – Ian Boyd Sep 01 '11 at 19:14
  • The best solution i've found is to use a source control product that supports shared files, and share them into the solution's local directory. This way we don't rely on Visual Studio to solve the problem, instead throwing it in the source control's lap. For better or worse, this ties us to Visual SourceSafe; as it's the only source control solution that supports sharing individual files. – Ian Boyd Sep 01 '11 at 19:25
  • Yes, C# makes it damned near impossible to write good (i.e. single `.exe`) Windows applications. – Ian Boyd Jul 06 '22 at 19:08
4

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.

alt text

RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • 2
    +1 I did not know of this feature. I would be inclined to say that this is not the better way to do this - though it is closer to the delphi way. Sharing through a class library is more in line with the .net way and if you fight that in the end it will bite you back IMO. – Neil Jan 20 '11 at 04:11
  • I think I'm glad I read the class library answer first, so that I'd have to do it the "right way". But this is good to know. Thanks – JosephStyons Jan 20 '11 at 04:35
  • The problem with Add As Link is that the path is now hard-coded in the project file. When other developers get the project from source control it will be unbuildable because they keep the common files in a different path than i do (or i keep the common files in a different path than they do). – Ian Boyd Sep 01 '11 at 19:30
2

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

Shekhar_Pro
  • 18,056
  • 9
  • 55
  • 79
  • A mess of linked files? In a Delphi project? Naa never happens :) . I went with option 3 in this case. Thanks for your thoughts. – JosephStyons Jan 20 '11 at 04:36
  • Problem with 2 and 3 is that a) not all developers have the dlls in the same path, and b) i have to ship a separate binary with my executable. – Ian Boyd Sep 01 '11 at 19:17
1

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.

Nate
  • 5,237
  • 7
  • 42
  • 52
  • Will Visual Studio compile a referenced .Net assembly (dll in this case) into my executable assembly - leaving me with one assembly? – Ian Boyd Sep 01 '11 at 19:18
  • Not by default, no. There are ways you can get all your assemblies into one file, but that is kind of a whole new topic... See http://stackoverflow.com/questions/476993/can-i-include-dll-in-exe-in-visual-studio – Nate Sep 02 '11 at 05:58