skip to Main Content

This is a more general question rather than an inquiry about a very specific technical point in some language. Imagine I write a set of very specific functions in VB.NET or maybe C# that take as input some data (say hourly temperature data) and perform complex calculations. Complex in the sense of many steps and checking conditions. So function A takes as input a matrix of 90×90 and spits out the determinant, for example, function B takes as input a part of that matrix and computes some average etc.

Now my question is this: how can I save the code behind these functions to be used in other projects? e.g. just like an Add-In in Excel. Would this be creating a DLL?I am using Visual Studio VB.NET. Should I make a console app and reference that (if so how?) in my future projects? I can write the code (currently transitioning from VBA) but I have a hard time to figure out this kind of "logistic" detail. Almost like a cook who knows the steps required to cook something but does not know his way around the kitchen:)

Thanks!

2

Answers


  1. Create a class library project. That can be referenced by other projects in the same solution as a project reference, or outside the solution as a compiled dll.

    For this simple example, in a project called ClassLibrary1, with this class

    namespace ClassLibrary1
    {
        public static class Class1
        {
            public static int PlusOne(int operand) => operand++;
            public static int MinusOne(int operand) => operand--;
        }
    }
    

    When adding a project reference, right click Dependencies and Add Project Reference…

    In this case, the project using it will be WinFormsApp1

    enter image description here

    Choose ClassLibrary1

    Then you can do this from within WinFormsApp1

    var a = ClassLibrary1.Class1.PlusOne(0);
    

    And you can also add a VB project the same way, and reference that class library again, and do

    Dim a = ClassLibrary1.Class1.MinusOne(1)
    

    If you build either of the apps, since the class library is a project reference, it will also be built automatically as needed. This is a benefit of using a project reference.

    Say you don’t want your class library project in the same solution as another project. Then you can directly reference the dll in the bin directory

    enter image description here

    It won’t be built automatically when projects using it are referencing it so you will need to initiate a build manually

    Login or Signup to reply.
  2. @djv is correct, use a Dynamic Link Library.

    Just add one to your solution (you’ll have 2 projects in the same tree) and (once you’ve referenced it) it will get updated on every build if its been changed (because it’s a dependency).

    You can either reference that in all your other projects by browsing to your original projects Debug/Release directory or add that project to any new solution you do (easier to debug on the fly that way).

    By the way, libraries are awesome!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search