问题描述
我的 MDIParent 中有一个 eterogene MDIChildForms (MyChild1,MyChild2,...) 的列表。所有这些表单都有一个公共函数 myFunction(p As myType)。我想循环它们,并为每个调用 myFunction。
伪代码:
For Each myChild In Me.MdiChildren
myChild.myFuntion(p)
Next
有可能吗?我怎样才能做到这一点?谢谢
解决方法
您应该定义一个接口来声明该方法,然后在每个子表单类中实现该接口。
Public Interface ISomeInterface
Sub SomeMethod()
End Interface
Public Class Form1
Implements ISomeInterface
Public Sub SomeMethod() Implements ISomeInterface.SomeMethod
'...
End Sub
End Class
然后您可以将每个表单转换为该类型并调用该方法,例如
For Each mdiChild As ISomeInterface In MdiChildren
mdiChild.SomeMethod()
Next
假设每个表单都实现了该接口。如果有些人没有,你可以这样做:
For Each mdiChild In MdiChildren.OfType(Of ISomeInterface)()
mdiChild.SomeMethod()
Next