Hi,
I have a base class that contains a fairly large number of parameters in it's New constructor. I have 7 subclasses that inherit the Super base class. My question/issue is, all of the subclasses use the same values for most of the parameters in the New constructor of the base class and these subclasses can be called one after the other. I would like to populate the common values for the superclass and then use those for each of the subclasses, but I can't come up with a good way to do this. If I could figure out how to do this, I wouldn't have to pass all of those parameters in the MyBase.New call for each subclass. I'll try to illustrate with some code ....
Public Class BaseRequest
Public Sub New(ByVal param1 As String, ByVal param2 As String, ByVal param3 As String, ByVal param4 As String, ByVal param5 As String, ByVal param6 As String, ByVal param7 As String, ByVal param8 As String, ByVal param9 As String, ByVal param10 As String)
'Private Level Variables Assigned Here'
End Sub
End Class
Public Class SubClass1
Inherits BaseRequest
Public Sub New(ByVal param1 As String, ByVal param2 As String, ByVal param3 As String, ByVal param4 As String, ByVal param5 As String, ByVal param6 As String, ByVal param7 As String, ByVal param8 As String, ByVal param9 As String, ByVal param10 As String)
MyBase.New(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10)
End Sub
End Class
Public Class SubClass2
Inherits BaseRequest
Public Sub New(ByVal param1 As String, ByVal param2 As String, ByVal param3 As String, ByVal param4 As String, ByVal param5 As String, ByVal param6 As String, ByVal param7 As String, ByVal param8 As String, ByVal param9 As String, ByVal param10 As String)
MyBase.New(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10)
End Sub
End Class
Public Class SubClass3
Inherits BaseRequest
Public Sub New(ByVal param1 As String, ByVal param2 As String, ByVal param3 As String, ByVal param4 As String, ByVal param5 As String, ByVal param6 As String, ByVal param7 As String, ByVal param8 As String, ByVal param9 As String, ByVal param10 As String)
MyBase.New(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10)
End Sub
End Class
Private Sub CallClasses
If CallClass1 Then
Dim sClass1 As New SubClass1(Param1Value, Param2Value, Param3Value, Param4Value, Param5Value, Param6Value, Param7Value, Param8Value, Param9Value, Param10Value)
End If
If CallClass2 Then
Dim sClass2 As New SubClass2(Param1Value, Param2Value, Param3Value, Param4Value, Param5Value, Param6Value, Param7Value, Param8Value, Param9Value, Param10Value)
End If
If CallClass3 Then
Dim sClass3 As New SubClass3(Param1Value, Param2Value, Param3Value, Param4Value, Param5Value, Param6Value, Param7Value, Param8Value, Param9Value, Param10Value)
End If
End Sub
I would like to get rid of the redundant calls to "MyBase.New" in each subclass and having to populate the parameters as I New up the calls to the SubClasses. In other words, do something like this ....
Dim MySuperClass as New BaseRequest(Param1Value, Param2Value, Param3Value, Param4Value, Param5Value, Param6Value, Param7Value, Param8Value, Param9Value, Param10Value)
If CallClass1 Then
Dim sClass1 As SubClass1
sClass1 = MySuperClass 'This Downcasting doesn't work, which is my problem.
End If
If CallClass2 Then
Dim sClass2 As SubClass2
sClass2 = MySuperClass 'This Downcasting doesn't work, which is my problem.
End If
If CallClass3 Then
Dim sClass3 As SubClass3
sClass3 = MySuperClass 'This Downcasting doesn't work, which is my problem.
End If
I hope that's understandable. What's the best way to do this?
-
Your current method is the best way of doing it. You should accept that and move on. The calls to MyBase.New aren't "redundant" — they each say something distinct, and without them, you wouldn't get the behavior you want. If you look at subclass constructors in the framework itself, you'll find it's done in the same way.
-
Harpo is correct.
However this situation sounds like a good candidate for the Factory design pattern. You could try encapsulating the object creation into another [Factory] class which would have a return type of BaseRequest. This factory method could then determine which type of object to create and you could then check the type of the returned object. e.g.
Public Class BaseRequestFactory Public Function CreateRequest(ByVal arg1 As Object,,,) As BaseRequest If condition1 Then Return New SubClass1(arg1,,,) Else If condition2 Then Return New SubClass2(arg1,,,) Else ''etc End If End Function End Class
One last thing; as you're probably realising, having methods with a large number of parameters is not a great practice. See if you can get away with using get/set properties instead, or maybe consider passing an instance of another class as a parameter to your constructor.
-
If you use inheritance there isn't really an alternative to passing the parameters down. However you might find that using composition is more appropriate. Try making your the base class concrete and passing it into the other classes as a parameter.
-
Harpo, Jim, Joel,
Thanks for your responses. I'll look more into the Factory Pattern, Joel.
Jim,> BlockquoteTry making your the base class concrete and passing it into the other classes as a parameter.
Blockquote I thought about going in this direction to, but I think it's best to go with the subclasses.
I would have left comments under each of your posts, but for some reason, StackOverflow doesn't think I asked this question .... and I don't have enough Rep yet to comment.
0 comments:
Post a Comment