Simple class library (dll) example?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kimiraikkonen

    #1

    Simple class library (dll) example?

    Hi there,
    I want to begin understanding how class libraries are written under
    VB.NET and how can i call them under my executable project. For
    example think an arithmetic calculator includes only plus, minus,
    division, multiplying functions.

    Yes it may not be necessary but how can i seperate each arithmetic
    funtion into per class library and call them under my executable
    project?

    Thanks...

  • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

    #2
    RE: Simple class library (dll) example?

    You will add a second project to your solution by right-clicking on the
    solution, then choosing "add->new project". Choose Class Library. You will
    add classes to this library as you require. The coding of the classes is no
    different than if it were in the executable project. From your executable,
    you add a reference to the project by normal means. The project tab will
    list the other projects that you may add as a reference.

    "kimiraikko nen" wrote:
    Hi there,
    I want to begin understanding how class libraries are written under
    VB.NET and how can i call them under my executable project. For
    example think an arithmetic calculator includes only plus, minus,
    division, multiplying functions.
    >
    Yes it may not be necessary but how can i seperate each arithmetic
    funtion into per class library and call them under my executable
    project?
    >
    Thanks...
    >
    >

    Comment

    • Tom Shelton

      #3
      Re: Simple class library (dll) example?

      On Nov 10, 6:59 am, kimiraikkonen <kimiraikkone.. .@gmail.comwrot e:
      Hi there,
      I want to begin understanding how class libraries are written under
      VB.NET and how can i call them under my executable project. For
      example think an arithmetic calculator includes only plus, minus,
      division, multiplying functions.
      >
      Yes it may not be necessary but how can i seperate each arithmetic
      funtion into per class library and call them under my executable
      project?
      >
      Thanks...
      You create a class library project containing your classes. Make the
      classes you want to expose, public as well as the methods you want to
      be visible out side of the assembly.

      For example the class we want to expose in the DLL:

      Option Strict On
      Option Explicit On

      Public Class Calculator
      Public Function Add(ByVal first As Integer, ByVal second As
      Integer) As Integer
      Return first + second
      End Function

      Public Function Subtract(ByVal first As Integer, ByVal second As
      Integer) As Integer
      Return first - second
      End Function
      End Class


      Now,in the project we want to use this class we need to make a
      reference to the dll. This can be done in several ways - but in this
      case, I'm going to use what's called a project reference. I'm going
      to go to the solution explorer right click on the solution -add ->
      Existing Project, and select my dll's project file. Once it's added
      to the solution, I'm going to go to the solution explorer and right
      click on the application I'm going to use the object from and select
      add reference. Once the add reference dialog comes up I'm going to
      select the projects tab and double click on my dll project.

      Once I do that, I can now add the imports statement - which will be
      for the namespace i put my object in, in this case I let it default,
      but in practice you should probably come up with a standardized way of
      doing your namespaces. I tend to do something like this
      companyname.cat egory.specificf unctionalgroup. So they might be, if my
      company was ABC something like: ABC.Utilities.W ordInterop Anway, this
      is the statement I add to my console app:

      Option Strict On
      Option Explicit On

      Imports ClassLibrary1 ' this is the namespace containing my caculator
      object

      Module Module1

      Sub Main()

      End Sub

      End Module
      >From then on we can just use it:
      Option Strict On
      Option Explicit On

      Imports ClassLibrary1

      Module Module1

      Sub Main()
      Dim calc As New Calculator

      Console.WriteLi ne("=========== === Adding ==============" )
      Console.WriteLi ne("4 + 3 = {0}", calc.Add(4, 3))
      Console.WriteLi ne("6 + 6 = {0}", calc.Add(6, 6))
      Console.WriteLi ne("-2 + 5 = {0}", calc.Add(-2, 5))
      Console.WriteLi ne("=========== =============== ==========")
      Console.WriteLi ne()
      Console.WriteLi ne("========== = Subtracting ===========")
      Console.WriteLi ne("2 - 2 = {0}", calc.Subtract(2 , 2))
      Console.WriteLi ne("5 - 8 = {0}", calc.Subtract(5 , 8))
      Console.WriteLi ne("100 - 3 = {0}", calc.Subtract(1 00, 3))
      Console.WriteLi ne("=========== =============== ==========")
      End Sub

      End Module

      Anyway - I hope that helps. You can also, use the browse tab and
      browse to the actual compiled dll. But, that can cause path issues
      especially in team development. Though, project references aren't
      always practicle either. The other way to reference the object is to
      put it in the gac - which means strong nameing the assembly. And then
      you will see it on the .NET references tab.

      --
      Tom Shelton

      Comment

      • kimiraikkonen

        #4
        Re: Simple class library (dll) example?

        On Nov 10, 6:28 pm, Tom Shelton <tom_shel...@co mcast.netwrote:
        On Nov 10, 6:59 am, kimiraikkonen <kimiraikkone.. .@gmail.comwrot e:
        >
        Hi there,
        I want to begin understanding howclasslibrari es are written under
        VB.NET and how can i call them under my executable project. For
        example think an arithmetic calculator includes only plus, minus,
        division, multiplying functions.
        >
        Yes it may not be necessary but how can i seperate each arithmetic
        funtion into perclasslibrary and call them under my executable
        project?
        >
        Thanks...
        >
        You create aclasslibrarypr oject containing your classes. Make the
        classes you want to expose, public as well as the methods you want to
        be visible out side of the assembly.
        >
        For example theclasswe want to expose in the DLL:
        >
        Option Strict On
        Option Explicit On
        >
        PublicClassCalc ulator
        Public Function Add(ByVal first As Integer, ByVal second As
        Integer) As Integer
        Return first + second
        End Function
        >
        Public Function Subtract(ByVal first As Integer, ByVal second As
        Integer) As Integer
        Return first - second
        End Function
        EndClass
        >
        Now,in the project we want to use thisclasswe need to make a
        reference to the dll. This can be done in several ways - but in this
        case, I'm going to use what's called a project reference. I'm going
        to go to the solution explorer right click on the solution -add ->
        Existing Project, and select my dll's project file. Once it's added
        to the solution, I'm going to go to the solution explorer and right
        click on the application I'm going to use the object from and select
        add reference. Once the add reference dialog comes up I'm going to
        select the projects tab and double click on my dll project.
        >
        Once I do that, I can now add the imports statement - which will be
        for the namespace i put my object in, in this case I let it default,
        but in practice you should probably come up with a standardized way of
        doing your namespaces. I tend to do something like this
        companyname.cat egory.specificf unctionalgroup. So they might be, if my
        company was ABC something like: ABC.Utilities.W ordInterop Anway, this
        is the statement I add to my console app:
        >
        Option Strict On
        Option Explicit On
        >
        Imports ClassLibrary1 ' this is the namespace containing my caculator
        object
        >
        Module Module1
        >
        Sub Main()
        >
        End Sub
        >
        End Module
        >
        From then on we can just use it:
        >
        Option Strict On
        Option Explicit On
        >
        Imports ClassLibrary1
        >
        Module Module1
        >
        Sub Main()
        Dim calc As New Calculator
        >
        Console.WriteLi ne("=========== === Adding ==============" )
        Console.WriteLi ne("4 + 3 = {0}", calc.Add(4, 3))
        Console.WriteLi ne("6 + 6 = {0}", calc.Add(6, 6))
        Console.WriteLi ne("-2 + 5 = {0}", calc.Add(-2, 5))
        Console.WriteLi ne("=========== =============== ==========")
        Console.WriteLi ne()
        Console.WriteLi ne("========== = Subtracting ===========")
        Console.WriteLi ne("2 - 2 = {0}", calc.Subtract(2 , 2))
        Console.WriteLi ne("5 - 8 = {0}", calc.Subtract(5 , 8))
        Console.WriteLi ne("100 - 3 = {0}", calc.Subtract(1 00, 3))
        Console.WriteLi ne("=========== =============== ==========")
        End Sub
        >
        End Module
        >
        Anyway - I hope that helps. You can also, use the browse tab and
        browse to the actual compiled dll. But, that can cause path issues
        especially in team development. Though, project references aren't
        always practicle either. The other way to reference the object is to
        put it in the gac - which means strong nameing the assembly. And then
        you will see it on the .NET references tab.
        >
        --
        Tom Shelton
        Very thanks Tom.

        Comment

        Working...