我有 3 个表格,每个表格都有自己的网格。以下代码当前是在 form1 上编写的,但被所有 3 个表单使用。它可以工作,但非常混乱且耗时很长,因为它必须适用于使用不同数据库并包含不同列的 3 个表单。将下面的代码拆分为 3 个单独的函数并放置在每个表单的背面,仅包含该特定表单所需的数据是否被认为更合适?
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
有人告诉我,最好将上面的代码放在每个表格的背面,并且只让它与它所在的表格一起工作。我觉得这不是最好的方法,但我也觉得它目前的设置方式也不好。什么是好方法?