locked
How do i create a common module, class, or library for use in multiple projects? RRS feed

  • Question

  • I have some common code in two projects. I am now creating a third project and would like to have a common library for these functions, so the code is in one place and updating one updates the other upon recompilation. I don't know if this needs to be a DLL or can simply be a class or other form of common code included in each of the projects. I am envisioning a single text file in a separate directory until some well-meaning devil dashes my dream of simplicity. The aim right now is to write it for the third project and perhaps incorporate it into the other two at a later date if and when they require significant change.

    How do i go about this? DLL, module, class? Separate file? Different directory? Copy to each project or include it in each one? And what option do i choose from the Visual Studio dialog? Create a new project? If so, which options? I think Visual Basic and Windows, but the third one...library?

    Monday, June 1, 2020 8:11 PM

Answers

  • Hello,

    First off a callback/delegate so long as the object that exposes the callback is visible to where you want to subscribe to it this is not an issue.

    Example, look at the following project. The delegate/callback is in a different namespace then the code which uses it, yes they are in the same project yet the point is a namespace separates them no different than two project namespaces.

    Note the callback is in the Modules namespace which uses FollowParent.Classes (that can be considered another project too)

    Imports FollowParent.Classes
    Namespace Modules
        Public Module DelegatesModule
            Public Delegate Function ValidPerson(Person1 As Person, Person2 As Person) As Boolean
            Public Delegate Sub FormMessageHandler(
                sender As Object, args As FormMessageArgs)
        End Module
    End Namespace

    Used above

    Namespace Classes
        Public Class FormMessageArgs
            Inherits EventArgs
    
            Protected _person As Person
            ''' <summary>
            ''' Expects a valid Person object with
            ''' First and Last name populated
            ''' </summary>
            ''' <param name="person"></param>
            Public Sub New(person As Person)
                _person = person
            End Sub
            ''' <summary>
            ''' Returns a Person object
            ''' </summary>
            ''' <returns></returns>
            Public ReadOnly Property Person() As Person
                Get
                    Return _person
                End Get
            End Property
            ''' <summary>
            ''' Determines if the person object is populated
            ''' </summary>
            ''' <returns></returns>
            Public ReadOnly Property IsValidPerson() As Boolean
                Get
                    Return _
                        Not String.IsNullOrWhiteSpace(_person.FirstName) AndAlso
                        Not String.IsNullOrWhiteSpace(_person.LastName)
                End Get
            End Property
        End Class
    End NameSpace

    Then in a form we create the event for the above callback as public scoped. Then later raise event is used to raise the event.

    In the calling form this is the event subscribed to the event above.

    Private Sub GetNewPerson(sender As Object, args As FormMessageArgs)
    	Dim people = CType(_bindingSource.DataSource, List(Of Person))
    
    	If people.FirstOrDefault(Function(p) PersonValid(p, args.Person)) Is Nothing Then
    		_bindingSource.Add(args.Person)
    	Else
    		MessageBox.Show($"{args.Person} exists.")
    	End If
    
    End Sub		

    If the above callback and event args were in a NuGet package the same would apply, it's all above scope and namespaces via import statements.

    So for you the two options I mentioned above are the best choices. We could add another option to create a .NET standard project which is what I used to create the NuGet package I created that is in my signature.

    https://www.nuget.org/packages/BaseConnectionLibrary/

    The issue here is it take more knowledge than the above, in my case I needed to make it compatible with many versions of the Framework. 


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Brian Tkatch Tuesday, June 2, 2020 4:52 PM
    Monday, June 1, 2020 10:57 PM

All replies

  • Looks like what i want is "Shared Project", which in my case is Visual Basic, Windows, Desktop. After i found the term, i was able to find a few articles talking about it, including Sharing is Caring: Using Shared Projects in ASP.NET and Shared Project : An Impressive Feature of Visual Studio 2015 Preview. Going to try that out. Does that sound right?
    Monday, June 1, 2020 8:31 PM
  • Hello,

    What I recommend is a class project that you reference in another project or many projects. In the following screenshot I have a class project which in this case is solely database operations but if I wanted too I could have anything I want. There are no code modules but if there were I would have a folder for code modules named Modules.

    Why use folders? Because visually it keeps things organized and at a deeper level you could have classes with the same names in different folder by wrapping them in different namespaces.

    There are two ways to do this.

    • Create a new solution in Visual Studio, create the class project, do your coding, build. Now back in the current solution add a reference to the DLL just created, done.
    • Create the class project in the current Visual Studio solution and reference it.

    You don't need a shared project.

    EDIT

    If you look at the following source code in one of my repositories

    https://github.com/karenpayneoregon/DescendantsVisalBasicWinForms

    The class projects are marked and could just as easily be in a different Visual Studio solution,


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Monday, June 1, 2020 9:43 PM
  • So, you are recommending a DLL?

    I started work on the Shared project, but it being able to include NuGet packages seems like a problem, so different solution might be better. (This is my first foray into this territory, so i don't quite know what i want until i try it and see it it works and then if it "works".)

    Also, one of the functions requires a callback (as it itself Handles an event from a NuGet package). Is that a simple thing to callback from a DLL or add a Handler?

    Monday, June 1, 2020 10:05 PM
  • Hello,

    First off a callback/delegate so long as the object that exposes the callback is visible to where you want to subscribe to it this is not an issue.

    Example, look at the following project. The delegate/callback is in a different namespace then the code which uses it, yes they are in the same project yet the point is a namespace separates them no different than two project namespaces.

    Note the callback is in the Modules namespace which uses FollowParent.Classes (that can be considered another project too)

    Imports FollowParent.Classes
    Namespace Modules
        Public Module DelegatesModule
            Public Delegate Function ValidPerson(Person1 As Person, Person2 As Person) As Boolean
            Public Delegate Sub FormMessageHandler(
                sender As Object, args As FormMessageArgs)
        End Module
    End Namespace

    Used above

    Namespace Classes
        Public Class FormMessageArgs
            Inherits EventArgs
    
            Protected _person As Person
            ''' <summary>
            ''' Expects a valid Person object with
            ''' First and Last name populated
            ''' </summary>
            ''' <param name="person"></param>
            Public Sub New(person As Person)
                _person = person
            End Sub
            ''' <summary>
            ''' Returns a Person object
            ''' </summary>
            ''' <returns></returns>
            Public ReadOnly Property Person() As Person
                Get
                    Return _person
                End Get
            End Property
            ''' <summary>
            ''' Determines if the person object is populated
            ''' </summary>
            ''' <returns></returns>
            Public ReadOnly Property IsValidPerson() As Boolean
                Get
                    Return _
                        Not String.IsNullOrWhiteSpace(_person.FirstName) AndAlso
                        Not String.IsNullOrWhiteSpace(_person.LastName)
                End Get
            End Property
        End Class
    End NameSpace

    Then in a form we create the event for the above callback as public scoped. Then later raise event is used to raise the event.

    In the calling form this is the event subscribed to the event above.

    Private Sub GetNewPerson(sender As Object, args As FormMessageArgs)
    	Dim people = CType(_bindingSource.DataSource, List(Of Person))
    
    	If people.FirstOrDefault(Function(p) PersonValid(p, args.Person)) Is Nothing Then
    		_bindingSource.Add(args.Person)
    	Else
    		MessageBox.Show($"{args.Person} exists.")
    	End If
    
    End Sub		

    If the above callback and event args were in a NuGet package the same would apply, it's all above scope and namespaces via import statements.

    So for you the two options I mentioned above are the best choices. We could add another option to create a .NET standard project which is what I used to create the NuGet package I created that is in my signature.

    https://www.nuget.org/packages/BaseConnectionLibrary/

    The issue here is it take more knowledge than the above, in my case I needed to make it compatible with many versions of the Framework. 


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Brian Tkatch Tuesday, June 2, 2020 4:52 PM
    Monday, June 1, 2020 10:57 PM