I have a problem in the VB.Net class library, which I greatly simplified to the next ...
Public MustInherit Class TargetBase
End Class
Public Class TargetOne
Inherits TargetBase
End Class
Public Class TargetTwo
Inherits TargetBase
End Class
Public Class TargetManager
Public Sub UpdateTargets(ByVal Targets As List(Of TargetBase))
For Each objTarget As TargetBase In Targets
UpdateTarget(objTarget)
Next
End Sub
Private Sub UpdateTarget(ByVal Value As TargetOne)
End Sub
Private Sub UpdateTarget(ByVal Value As TargetTwo)
End Sub
End Class
This will not be compiled due to a syntax error in the string UpdateTarget(objTarget). Overload resolution error because the available "UpdateTarget" cannot be called without narrowing the conversion.
So, I changed the For-Each loop to use Object instead of TargetBase ...
For Each objTarget As Object In Targets
UpdateTarget(objTarget)
Next
Now it compiles, but I get a runtime error. Public element UpdateTarget on type "TargetManager" not found.
So, I took the next step by making 2 UpdateTarget () an overload of Public (instead of Private).
Public Sub UpdateTarget(ByVal Value As TargetOne)
End Sub
Public Sub UpdateTarget(ByVal Value As TargetTwo)
End Sub
Now it works!
, Object , , - , .
- ?
( !)
. Ive ( UpdateTarget Public), . TypeOf objTarget, DirectCast UpdateTarget, ...
For Each objTarget As Object In Targets
If TypeOf objTarget Is TargetOne Then
UpdateTarget(DirectCast(objTarget, TargetOne))
ElseIf TypeOf objTarget Is TargetTwo Then
UpdateTarget(DirectCast(objTarget, TargetTwo))
End If
Next
- , , UpdateTarget Private to Public , !