過負荷はまったく異なる概念です。本質的に、オーバーロードにより、メソッドをオーバーライドして元の動作を「置換」しながら、複数のAdd()
andRemove()
メソッドを使用できます。シャドーイングはその中間です。シャドウイングは元の動作を「隠します」が、基本型にキャストすることで基本メソッドにアクセスできます。
一般的に言えば、代わりにオーバーライドできる場合は、メンバーをシャドウしません。よほどの特別な理由がない限り。
同じメンバーをオーバーライドしてシャドウすることはできないことに注意してください。ただし、たとえば、元のAdd()
メソッドのオーバーライドとオーバーロードの両方を行うことができます。
編集
Add()
メソッドをオーバーライドすることはできませんが、オーバーライドすることはできますOnAddingNew()
。同様に、 をオーバーライドすることはできませんがRemove()
、オーバーライドすることはできますRemoveItem()
。このクラスの詳細はわかりませんが、裏でRemove()
使用RemoveItem()
されていると思われます。
実装は次のようになります。
Imports System.ComponentModel
Imports System.Collections.ObjectModel
Public Class DeviceUnderTestBindingList
Inherits BindingList(Of DeviceUnderTest)
Private Sub New()
MyBase.New()
End Sub
#Region "Add()"
Protected Overrides Sub OnAddingNew(e As AddingNewEventArgs)
Dim newDevice As DeviceUnderTest = DirectCast(e.NewObject, DeviceUnderTest)
Try
If (Not IsValidEntry(newDevice)) Then ' don't add the device to the list
Exit Sub
End If
' (optionally) run additional code
DoSomethingWith(newDevice)
Finally
MyBase.OnAddingNew(e)
End Try
End Sub
Private Function IsValidEntry(device As DeviceUnderTest) As Boolean
' determine whether the specified device should be added to the list
Return True Or False
End Function
Private Sub DoSomethingWith(newDevice As DeviceUnderTest)
' This is where you run additional code that you would otherwise put in the 'Add()' method.
Throw New NotImplementedException
End Sub
#End Region ' Add()
#Region "Remove()"
Public Shadows Function Remove(device As DeviceUnderTest) As Boolean
Try
RemoveItem(IndexOf(device))
Catch
Return False
End Try
Return True
End Function
Protected Overrides Sub RemoveItem(index As Integer)
MyBase.RemoveItem(index)
End Sub
#End Region ' Remove()
End Class