Working on the basis that all repetition is bad, is there a good way to write code to call constructors for a range of class types all deriving from a common base? e.g.
I know you can use Activator.CreateInstance but then there's no check on whether the array that you're sending to the constructor is valid until runtime. So any thoughts?
Visual Basic:
Dim h As base
If x = 0 Then
h = New base(1, "ABC")
ElseIf x < 0 Then
h = New inheritor1(1, "ABC")
ElseIf x > 0 Then
h = New inheritor2(1, "ABC")
End If
Public Class base
Public Sub New(ByVal x As Long, ByVal y As String)
End Sub
End Class
Public Class inheritor1
Inherits base
Public Sub New(ByVal x As Long, ByVal y As String)
MyBase.New(x, y)
End Sub
End Class
Public Class inheritor2
Inherits base
Public Sub New(ByVal x As Long, ByVal y As String)
MyBase.New(x, y)
End Sub
End Class
I know you can use Activator.CreateInstance but then there's no check on whether the array that you're sending to the constructor is valid until runtime. So any thoughts?