英文:
Pass both Form and Data Grid View as parameters
问题
我有一个名为 Sub
的函数,它接受 Form
和 DataGridView
作为参数。
Public Sub ExecuteReaderDataGridView(SQLQuery As String,
TargetDataGridView As DataGridView,
TargetForm As Form)
到目前为止,我同时使用了这两个参数,但现在需要根据情况使用其中之一。
是否有一种方法可以避免创建两个不同的 Sub
函数,一个带有 DataGridView
作为参数,另一个带有 Form
作为参数?
英文:
I have a Sub
which takes both Form
and DataGripView
as parameters.
Public Sub ExecuteReaderDataGridView(SQLQuery As String,
TargetDataGridView As DataGridView,
TargetForm As Form)
Up to now, I use both parameters but now there is a need to use one of the two depends on the case.
Is there a way to avoid create two Subs
one with DataGridView
as a parameter and one with Form
?
答案1
得分: 3
Utilize function overloading:
' Sub that takes the query and a DGV:
使用函数重载:
' 接受查询和DataGridView的子程序:
Public Sub ExecuteReaderDataGridView(SQLQuery As String, TargetDataGridView As DataGridView)
ExecuteReaderDataGridView(SQLQuery, TargetDataGridView, Nothing)
End Sub
' Sub that takes the query and a Form:
' 接受查询和表单的子程序:
Public Sub ExecuteReaderDataGridView(SQLQuery As String, TargetForm As Form)
ExecuteReaderDataGridView(SQLQuery, Nothing, TargetForm)
End Sub
' Your original Sub:
' 您的原始子程序:
Public Sub ExecuteReaderDataGridView(SQLQuery As String, TargetDataGridView As DataGridView, TargetForm As Form)
' You'll need to modify your original Sub by adding code
' to determine if TargetDataGridView or TargetForm is Nothing.
' 您需要通过添加代码来修改原始子程序,以确定TargetDataGridView或TargetForm是否为Nothing。
End Sub
英文:
Utilize function overloading:
' Sub that takes the query and a DGV:
Public Sub ExecuteReaderDataGridView(SQLQuery As String, TargetDataGridView As DataGridView)
ExecuteReaderDataGridView(SQLQuery, TargetDataGridView, Nothing)
End Sub
' Sub that takes the query and a Form:
Public Sub ExecuteReaderDataGridView(SQLQuery As String, TargetForm As Form)
ExecuteReaderDataGridView(SQLQuery, Nothing, TargetForm)
End Sub
' Your original Sub:
Public Sub ExecuteReaderDataGridView(SQLQuery As String, TargetDataGridView As DataGridView,TargetForm As Form)
' You'll need to modify your original Sub by adding code
' to determine if TargetDataGridView or TargetForm is Nothing.
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论