我有如下代码。这是主要的逻辑功能,我想在该过程中插入其他过程。这样做是一种方法还是解决方案。我用**标记。
Public Shared Sub CheckListSubstrs(ByVal Substrs As IScrNamedObjectList, **mySub(Substr As IScrSubstructure)**)
Dim Substr As IScrSubstructure = Nothing
Dim nSubstr As Integer = Nothing
nSubstr = Substrs.count
If nSubstr > 0 Then
For i As Integer = 0 To nSubstr - 1
Substr = CType(Substrs.item(i), IScrSubstructure)
**mySub(Substr As IScrSubstructure)**
Next
End If
End Sub
我有不同类型的sub / func过程,它们全部使用Substr As IScr as Substructure
作为输入,因此我想动态地插入它们并为不同的类,模块调用它们。
编辑
我必须澄清更明确的问题,以简化对话。
这是我所有的课程。
Option Explicit On
Option Strict On
Imports simpackcomslvLib
Public Class Substr
Public Shared Sub CheckListSubstrs(ByVal Substrs As IScrNamedObjectList, ByVal dgv As DataGridView, SourceType As ****)
Dim nSubstr As Integer = Nothing
nSubstr = Substrs.count
If nSubstr > 0 Then
For i As Integer = 0 To nSubstr - 1
Dim Substr As IScrSubstructure = CType(Substrs.item(i), IScrSubstructure)
'Procedure comes here according to element type for example listing bodies
' CheckListBodies(Substr.getBodyList(False), DataGridView2)
'or if i list forces
'CheckListForces(Substr.getForceList(False), DataGridView3)
'Recursive usage function to get lower substructures information you can think there's a cascaded structure of substructures
CheckListSubstrs(Substrs:=Substr.getSubstrList(False), ProcedureForElementType As ****)
Next
End If
End Sub
Private Shared Sub CheckListBodies(ByVal Bodies As IScrNamedObjectList, ByVal dgv As DataGridView)
Dim nBody As Integer
nBody = Bodies.count
For i As Integer = 0 To nBody - 1
Dim Body As IScrBody = CType(Bodies.item(i), IScrBody)
dgv.Rows.Add(Body.fullName)
Next
End Sub
Private Shared Sub CheckListForces(ByVal Forces As IScrNamedObjectList, ByVal dgv As DataGridView)
Dim nForce As Integer
nForce = Forces.count
For i As Integer = 0 To nForce - 1
Dim Force As IScrForce = CType(Forces.item(i), IScrForce)
dgv.Rows.Add(Force.fullName)
Next
End Sub
Public Shared Sub RunTheCodeforBodies()
CheckListSubstrs(Mdl.getSubstrList(False), DataGridView2, getBodyList)
End Sub
Public Shared Sub RunTheCodeforForces()
CheckListSubstrs(Mdl.getSubstrList(False), DataGridView3, getForceList)
End Sub
End Class
正如我在此处显示的两个示例所示,我列出了大约不同的类型。 30种。我正在使用com-interface和我正在连接的第三部分软件中的 Iscr 类型的类。
所以所有属性都属于子结构,我只想更改功能元素类型并输出datagridview。
答案 0 :(得分:1)
由于您已经具有应详细说明 IScrSubstructure
对象的现有方法,并且正如您所说的,所有方法都具有相同的签名,因此您可以使用具有相同签名的method delegate,并将其用作CheckListSubstrs
子级的参数。
模拟,其中包含一些可用于测试的对象:
Public Structure IScrSubstructure
Public value1 As String
Public value2 As Integer
End Structure
Public Class IScrNamedObjectList
Inherits List(Of IScrSubstructure)
End Class
Public Delegate Sub ScrSubstructureDelegate(ByVal Substr As IScrSubstructure)
Public Shared Sub CheckListSubstrs(ByVal Substrs As IScrNamedObjectList, MyDelegate As ScrSubstructureDelegate)
If Substrs?.Count > 0 Then
For Each item As IScrSubstructure In Substrs
MyDelegate(item)
Next
End If
End Sub
现在,您的CheckListSubstrs
方法具有一个参数:
MyDelegate As ScrSubstructureDelegate
您可以传递与该签名匹配的任何方法:
ByVal Substr As IScrSubstructure
如果您尝试传递与委托签名不匹配的方法,则代码将无法编译。
因此,让我们构建具有这些特征的几个方法,并使用这两个方法作为 CheckListSubstrs
参数调用MyDelegate
方法:
Public Sub MyIScrSub(ByVal Substr1 As IScrSubstructure)
'Do something with Substr1
Console.WriteLine("MyIScrSub Value1: {0}, MyIScrSub Value2: {1}", Substr1.value1, Substr1.value2)
End Sub
Public Sub MyOtherIScrSub(ByVal AnotherSubscr As IScrSubstructure)
'Do something with AnotherSubscr
Console.WriteLine("MyOtherIScrSub Value1: {0}, MyOtherIScrSub Value2: {1}", AnotherSubscr.value1, AnotherSubscr.value2)
End Sub
现在,您可以调用CheckListSubstrs
并通过 MyIScrSub
和 MyOtherIScrSub
方法作为委托:
Dim ScrList As IScrNamedObjectList = New IScrNamedObjectList()
ScrList.Add(New IScrSubstructure() With {.value1 = "Value1", .value2 = 1})
ScrList.Add(New IScrSubstructure() With {.value1 = "Value2", .value2 = 2})
ScrList.Add(New IScrSubstructure() With {.value1 = "Value3", .value2 = 3})
CheckListSubstrs(ScrList, AddressOf MyIScrSub)
CheckListSubstrs(ScrList, AddressOf MyOtherIScrSub)
作为注释,我在CheckListSubstrs
子节中写道:
If Substrs?.Count > 0 Then
(...)
End If
因此您可以处理 IScrNamedObjectList
参数的空值:
(此语法需要VB.Net 14
或更高版本)
CheckListSubstrs(nothing, AddressOf MyIScrSub)
但是您也可以写:
If Substrs IsNot Nothing Then
(...)
End If