Link to home
Start Free TrialLog in
Avatar of gwosgood
gwosgoodFlag for United States of America

asked on

The best way to design portable self-contained modular code

Greetings Experts,

I am on the ground-level for a rather large project spanning over a number of related applications.  One of the similar requirements that each of these apps require is a "Quick Link" list of buttons that, when clicked, launch the other related .exes.  I have designed a simple GroupBox containing the six mentioned buttons.  I need a way to encapsulate the corresponding code linked to these buttons into a module, that I can copy and paste into then distribute to the individual project files.  I think it would be much cleaner and easier to handle to copy just one file, rather than 6 button's worth of code back and forth between projects.

Is what I am asking possible to do?  Please advise me on the proper way to go about doing this.   Thank you for your attention and advice.


Sample Button Code im looking to store in module:


Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles MyBase.Load
 
        ' <<button startup>>
        Dim filepath As String
        Dim fi As IO.FileInfo
 
        filepath = "C:\documents and settings\all users\desktop\app1.exe"
        fi = New IO.FileInfo(filepath)
        If fi.Exists Then Button1.Enabled = True
 
        filepath = "C:\documents and settings\all users\desktop\app2.exe"
        fi = New IO.FileInfo(filepath)
        If fi.Exists Then Button1.Enabled = True
        ' /<button startup>>
 
    End Sub
 
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Button1.Click
 
        Dim filepath As String = "C:\documents and settings\all users\desktop\app1.exe"
        Dim appName As String = filepath.Substring(filepath.LastIndexOf("\") + 1)
        Try
            System.Diagnostics.Process.Start(filepath)
        Catch ex As Exception
            MessageBox.Show("An error has occured while trying to launch " & appName & vbCrLf & _
                            "Error Message: " & ex.Message & vbCrLf & _
                            "Error Details: " & ex.InnerException.ToString, "Error")
        End Try
 
    End Sub

Open in new window

Avatar of omegaomega
omegaomega
Flag of Canada image

Hello, gwosgood,

Re:
"I think it would be much cleaner and easier to handle to copy just one file, rather than 6 button's worth of code back and forth between projects."

I fully agree.  Any time you are copying code from one place to another, it is likely to cause a future maintenance problem.  It is also a sign of an opportunity to generalize and centralize the code in question, which is what you want to do.

In this particular case, I would probably convert (or encapsulate) the GroupBox into a user control and distribute that control to the projects in question.

(btw, in the second paragraph of your frmMain_Load sample code where you check the existence of app2.exe, do you actually mean to enable Button2?)

Cheers,
Randy
I agree with Randy... sort of.

Whenever I hear "cut and paste," I shudder. This is a recipe for pain in maintenance.

Encapsulate the functionality as a user control, as Randy said. However, I would tend to create a project reference to the user control project (assuming it is in some sort of source control). This will update the user control in all applications when the user control is updated.

If you'd prefer not to have to test all applications when the user control changes, then compile and distribute the DLL containing the control.
Avatar of gwosgood

ASKER

Omegaomega,

Hi, thank you for your comment.  I am glad you agree with my strategy to 'encapsulate' the groupbox's code within a modular format.  I guess I was looking for more of an example of tying the controls to the module, and they referencing them within the application.

In reference to the app2.exe question, yes, thank you, it was a classic example of copy-n-paste error.  This is exactly the type of thing I would like to avoid with the Quick List bar!   :)
I understand the concepts of the module well enough, but I am having difficulties actually designing the code.  Can someone please show me a sample of module code related to my situation, and an example of how to implement it within my main application?
ASKER CERTIFIED SOLUTION
Avatar of omegaomega
omegaomega
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Omegaomega,


Thank you for pointing me in the right direction.  In the end, I had to scrap my project and create a new one, using a Control Library project type instead of the Windows Application project I had been designing.  After porting my code over to the new format, I was able to run and save my user control to a .dll file, which I then imported into the toolbox.

Thank you for your assistance

gwosgood