Garrettchap1
Garrettchap1

Reputation: 79

Code organization and reusability in this situation?

I have 3 forms and they each have their own grids. The following code is currently written on form1 but is used by all 3 forms. It works but is very messy and long since it must work for 3 forms that use different databases and contain different columns. Would it be considered more proper to split the code below into 3 separate functions and place on the back of each form containing only the data needed for that specific form ?

Public Class Form1

Public sFilter as string
Public sQuery as string


Public Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load    

Public Sub buildFilter(ByVal form as string)
  select case form 
    Case "form1"
    sFilter = "control_no > 0 and control_no < 10000" 
    Case "form2"
    sFilter = "quantity > 0 and quantity < 7849"
    Case "form3"
    sFilter = "store_id > 10000"
  end select
End Sub

Public Sub buildQuery(form)
  Select case form
    Case "form1"
    sQuery = "Select * FROM dataBase1 WHERE " & sFilter 
    Case "form2"
    sQuery = "Select quantity, first_name, last_name FROM database2 WHERE " & sFilter
    Case "form3"
    sQuery = "Select * FROM dataBase3 WHERE " & sFilter
  End Select
End Sub


End  Class

I was told it would be best to take the above code and place it on the back of each form and only have it work with the form its on. I feel like that isn't the best approach, but I also feel that the way its currently setup isn't good either. What would be a good approach?

Upvotes: 0

Views: 103

Answers (2)

Joel Etherton
Joel Etherton

Reputation: 37533

Build these methods into a common inherited form and then inherit it by your other forms:

Public MustInherit Class MyInheritedForm
    Inherits System.Windows.Forms.Form

    Public sFilter as string
    Public sQuery as string


    Public Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load    

    Public Sub buildFilter(ByVal form as string)
      select case form 
        Case "form1"
        sFilter = "control_no > 0 and control_no < 10000" 
        Case "form2"
        sFilter = "quantity > 0 and quantity < 7849"
        Case "form3"
        sFilter = "store_id > 10000"
      end select
    End Sub

    Public Sub buildQuery(form)
      Select case form
        Case "form1"
        sQuery = "Select * FROM dataBase1 WHERE " & sFilter 
        Case "form2"
        sQuery = "Select quantity, first_name, last_name FROM database2 WHERE " & sFilter
        Case "form3"
        sQuery = "Select * FROM dataBase3 WHERE " & sFilter
      End Select
    End Sub


End  Class

Then you just need:

Public Class Form1
    Inherits MyInheritedForm


End Class

And your methods come right along with the inheritance.

Upvotes: 1

Matt Wilko
Matt Wilko

Reputation: 27322

There is no point having all that code in three forms.

One option is to use one form with the code you already have and set a property of the form depending on which connection you are using.

If the three forms are different in design and are connecting to different databases then why not just have three different forms with three different sets of query and filter code?

Side note: You should also have a look at parametrised queries

Upvotes: 1

Related Questions