I wanted to know if it is possible to reuse a class I created in one application in another application.
Basically I created a test program and once I got all the co开发者_StackOverflow社区mponents working I created a class for it and well I want to use the class in another program, how can I do this?
I'm a nub when it comes to programming so if you can make it as easy to understand a possible I would be greatful.
There are a couple of common ways, depending on how you want to organize your code/projects:
- Place the class in a class library project and reference that project in your other projects.
- Place the class in a class library, compile it, reference the resulting DLL in your other projects.
- Copy the class file to another project. (This results in duplicating the class across projects, but there do exist situations that call for this.)
- Reference the project which contains the class in another project. (If the project being referenced is an application, this is unfavorable. It would work, but it's poor code organization.)
(1) is my personal preference, I like to have many projects in my solutions which reference each other's code directly rather than referencing compiled DLLs. A lot of people prefer (2) for their own code, not wanting to keep so many projects open all over the place and rather deal with a one-time compiled DLL. It's a matter of personal preference.
Your best bet is to have that class in an assembly.
Make a new project - a Windows Class Library. Put your class to be re-used in there.
Reference this project or built assembly from your application(s).
You'll be able to then Imports
the namespace in your consuming applications. Naming becomes important here, but try it out, and see how it works for you.
精彩评论