开发者

Visual Basic How do you Reuse Classes

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:

  1. Place the class in a class library project and reference that project in your other projects.
  2. Place the class in a class library, compile it, reference the resulting DLL in your other projects.
  3. Copy the class file to another project. (This results in duplicating the class across projects, but there do exist situations that call for this.)
  4. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜