Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all,
Vanilla are planning an update to the site on April 24th (next Wednesday). It is a major PHP8 update which is expected to boost performance across the site. The site will be down from 7pm and it is expected to take about an hour to complete. We appreciate your patience during the update.
Thanks all.

Query on structuring project (Classes v Modules)

Options
  • 07-03-2014 1:04am
    #1
    Closed Accounts Posts: 1,155 ✭✭✭


    Hi all,

    I am trying to understand when to use Classes v Modules. I'll use an example to illustrate my problem. Language is VB.NET.

    Within my Form class, I have a background worker. The worker is kicked off by a button press. Depending on what is displayed on the form, I need to do a specific job. I determine what the job is and pass this info to the worker as an argument.

    The background worker is required to allow a user to still use the form as the processes can be quite time consuming.

    So, here is the background worker DoWork event;
    Private Sub BackgroundWorker1_DoWork
    (ByVal sender As System.Object	ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    
       Select Case e.Argument
    
            Case "job1"
                 ProcessingJob_1()
            Case "job2"
                 ProcessingJob_2()
     
       End Select
    
    End Sub
    

    The ProcessingJob_x are subs in a module. The jobs run queries etc and load a public DataTable object.

    After this, I need to update the GUI (form) with the DataTable results (I cannot do this in ProcessingJob as that would cause a cross-thread issue). As with ProcessingJob_x, what happens exactly depends on the circumstances. So, similarly to the DoWork event, I use the RunWorkerCompleted event like so;
    Private Sub BackgroundWorker1_RunWorkerCompleted(....)
    
        Select Case yyy
    
           Case "job1"
               ProcessingJob_1_Post()
           Case "job2"
               ProcessingJob_2_Post()
    
        End Select
    
    End Sub
    

    As before, ProcessingJob_x_Post() are subs within a module.

    So...what is my problem....

    I guess it all feels a bit...I dunno...wrong! I am using OOP and I don't quite understand when to use classes to create objects.

    I am using modules only and it all seems a bit VB6.

    I guess I am looking for guidance from the experts on how yee would accomplish implementing the above concept.

    Many thanks for your time.


Comments

  • Registered Users Posts: 1,712 ✭✭✭neil_hosey


    Modules are a throwback to VB6 where they were essentially static classes in c#/java/etc...

    Like a static class, modules should just be used for helper methods only in my opinion. The main problem with modules is if you depend on them highly to house your core business logic, you break the rule "Open for extension, closed for modification" pretty quickly when new additions to business logic comes along. This is because you can't subclass a module.

    The advantage of using a class here for each ProcessingJob is that you could use the Factory method pattern to retrieve which class (have one class for each ProcessingJob_x which inherits from a base IProcessingJob interface)

    Then you can use delegates and apply the observer pattern to solve your problem of cross thread communication, see here for an example:
    http://www.codeproject.com/Articles/5754/Implementing-Observer-Pattern-in-VB-NET


  • Closed Accounts Posts: 1,155 ✭✭✭Stainless_Steel


    Just getting back to my thread now.

    Thanks for that Neil. To be honest it is a bit over my head...I have a lot of reading to do!

    Any other feedback appreciated.


Advertisement