Results 1 to 7 of 7

Thread: Create code that can be used in multiple projects (dll?) (Resolved)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Create code that can be used in multiple projects (dll?) (Resolved)

    What is the process for making a function into a dll(?) that can be used in multiple projects? Let me use the extremely simple function as an example.

    VB Code:
    1. Public Function DoCalcs( _
    2.         ByVal a As Double, _
    3.         ByVal b As Double, _
    4.         ByVal c As Double, _
    5.         ByVal d As Double) As Double
    6.  
    7.         Dim myCalc As Double = (a * b / c - d)
    8.         Return myCalc
    9.  
    10.     End Function
    How would I make it so that I can add this function to any project using a reference (or whatever)?
    Last edited by FastEddie; Mar 31st, 2006 at 05:02 PM.

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Create code that can be used in multiple projects (dll?)

    you simply change the output to class library (Right click project, go to properties, change "output type" to class library). When it compiles, it creates a dll, which you can just reference. I think the default namespace would be the name of the assembly, although you can create a namespace using the "namespace" keyword...

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Create code that can be used in multiple projects (dll?)

    you simply change the output to class library (Right click project, go to properties, change "output type" to class library). When it compiles, it creates a dll, which you can just reference.
    That is what I thought so I must be doing something else wrong. Should the function be stored in a class file or a module (or something else)?

    Also, once done, all I need to do is to add that created dll as a reference correct?

  4. #4
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Create code that can be used in multiple projects (dll?)

    Not sure how modules would work with it, as I have only made class libraries out of full class files. All you would have to do is add a new class to whatever you are doing this in, and name the class what you want, put the function code in there (along with any other functions you want to use), then build it. Then its a matter of referencing the dll, declaring a new instance of the class that contains the function, then using the function.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Create code that can be used in multiple projects (dll?)

    Ok I almost have it. My Class is named doACalc and the function is DoCalcs and the code block that I want to use it looks like this
    VB Code:
    1. Dim d1 As Double = CDbl(tb1.Text)
    2.         Dim d2 As Double = CDbl(tb2.Text)
    3.         Dim d3 As Double = CDbl(tb3.Text)
    4.         Dim d4 As Double = CDbl(tb4.Text)
    5.         Dim Answer As String = DoCalcs(d1, d2, d3, d4).ToString
    6.         Label1.Text = Answer
    What is the proper way to get the new instance in this block?

  6. #6
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Create code that can be used in multiple projects (dll?)

    a form is a class, so how would you go about declaring a new instance of a form? Its the same way. Since you are referencing this dll (remember to add a reference to the dll in your project), you just use the namespace to navigate to that class in intellisense...
    VB Code:
    1. 'replace "MyAssemblyNamespace" assembly name of your dll, or the namespace you defined in the class...
    2. Dim MyNewClass as New MyAssemblyNamespace.doACalc
    3. MyNewClass.Docalcs()
    You may not even have to declare a new instance of the class, you might be able to get away with just "MyAssemblyNamespace.doACalc.Docalcs()", but I am not positive...
    Last edited by gigemboy; Mar 31st, 2006 at 04:51 PM.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Create code that can be used in multiple projects (dll?)

    Thanks Gig. For those of you following along this is how it ended up looking.
    VB Code:
    1. Dim d1 As Double = CDbl(tb1.Text)
    2.         Dim d2 As Double = CDbl(tb2.Text)
    3.         Dim d3 As Double = CDbl(tb3.Text)
    4.         Dim d4 As Double = CDbl(tb4.Text)
    5.  
    6.         Dim MyNewClass As New DoCalcs.Buck.doAcalc
    7.         Label1.Text = MyNewClass.DoCalcs(d1, d2, d3, d4).ToString

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