CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Hybrid View

  1. #1
    Join Date
    May 2009
    Posts
    34

    Starting a Project Where I know I'll want to reuse my functions in other programs

    Disclaimer: I'm not new to programming, but still quite the novice in C#. Background heavy in VB6.

    Scenario: I'm going to start a new project that does a bunch of Astronomical calculations. I know that many of the functions that I'm going to write for this project I will want to use again for other projects down the road.

    In VB6, I would simply create a MODULE and add a bunch of GLOBAL functions and subroutines. This MODULE can then be loaded into any future VB6 project to allow me to reuse that code.

    In VS2010 C#, I'm not sure what the equivalent of doing this happens to be. When I do a search on "creating a library" in C#, like when I would do so in C and use "include..." statements to add 'header' files like "mystuff.h", it appears that the word "library" carries a different meaning in C# as it gives me all kinds of information on creating DLL's.

    I don't want to create a DLL. I just want an easy way to reuse my code in one app in another app.

    QUESTION: To reuse functions written for one application in future applications, what is the 'best practice' approach to do this?

    Is this adding a CLASS? Do I have to do something called DELEGATES?

    I'm puzzled about how to start.

    I don't want to start writing this project one way only to find out that I should have started another way. So that is why I am asking now BEFORE I start adding any files or code to my new project.

    Suggestions?

    TIA

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Starting a Project Where I know I'll want to reuse my functions in other programs

    In C# classes are contained within assemblies. The assembly can be an exe or a dll. If it's a dll, it's called a class library. Generally, you put reusable classes in a class library. Alternatively, you can put classes into cs files and then include them in your project by adding the file to your project as a link. This is definitely not the recommended practice. That being said, why don't you want to create a dll?

  3. #3
    Join Date
    May 2009
    Posts
    34

    Re: Starting a Project Where I know I'll want to reuse my functions in other programs

    Quote Originally Posted by Arjay View Post
    In C# classes are contained within assemblies. The assembly can be an exe or a dll. If it's a dll, it's called a class library. Generally, you put reusable classes in a class library. Alternatively, you can put classes into cs files and then include them in your project by adding the file to your project as a link. This is definitely not the recommended practice. That being said, why don't you want to create a dll?
    Well, I've never done it before. It's not that I have anything against it, but it has always been easy for me to have a set of header files (when I programmed in C) and modules (when I program in VB6). Then I just add them.

    So in C#, if I understand your reply correctly, my functions are to all be part of a class or classes. And I can just create separate .cs files and add them to the project much like I have done with modules.

    Okay, I just wanted to make sure.

    This is just to start off. I'm going through a book on Astronomy formulas and will need to create a bunch of functions. Just didn't want to waste the effort as I know I'll need them again in other programs. Perhaps I can look into creating DLLs in the future after I've successfully programmed these functions.

    Thanks.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Starting a Project Where I know I'll want to reuse my functions in other programs

    You asked for best practices and adding shared files to the project isn't a best practice. Honestly, it's worth the time spent now to create an assembly and we're only talking about maybe 5 minutes to create a solution that contains your application and a library.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured