传递表单和数据网格视图作为参数。

huangapple go评论93阅读模式
英文:

Pass both Form and Data Grid View as parameters

问题

我有一个名为 Sub 的函数,它接受 FormDataGridView 作为参数。

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

huangapple
  • 本文由 发表于 2023年5月10日 21:51:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219245.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定