Constructor in MustInherit Class
-
I have a MustInherit class A, something like
Public MustInherit Class A
Protected A as Integer Public Sub New(ByVal X As Integer) A=X End Sub Public MustOverride Sub B()
End Class
and I want the declared constructor to be used for any derived classes. However, in derived classes, like
Public Class D
Inherits APublic Sub B() A+=2 End Sub
End Class
VS gives me an error, saying that there is no constructor in A that can be called without any arguments. Is there any way I can inherit the constructor?
~ Soumya92
-
I have a MustInherit class A, something like
Public MustInherit Class A
Protected A as Integer Public Sub New(ByVal X As Integer) A=X End Sub Public MustOverride Sub B()
End Class
and I want the declared constructor to be used for any derived classes. However, in derived classes, like
Public Class D
Inherits APublic Sub B() A+=2 End Sub
End Class
VS gives me an error, saying that there is no constructor in A that can be called without any arguments. Is there any way I can inherit the constructor?
~ Soumya92
Soumya92 wrote:
VS gives me an error, saying that there is no constructor in A that can be called without any arguments.
Let's add that constructor then, and override the
B
sub;Public Class D
Inherits APublic Sub New(ByVal X As Integer) MyBase.New(X) End Sub Public Overloads Overrides Sub B() A = A + 2 End Sub
End Class
I are Troll :suss:
-
I have a MustInherit class A, something like
Public MustInherit Class A
Protected A as Integer Public Sub New(ByVal X As Integer) A=X End Sub Public MustOverride Sub B()
End Class
and I want the declared constructor to be used for any derived classes. However, in derived classes, like
Public Class D
Inherits APublic Sub B() A+=2 End Sub
End Class
VS gives me an error, saying that there is no constructor in A that can be called without any arguments. Is there any way I can inherit the constructor?
~ Soumya92
If class D inherits From Class A and Class A has no paramterless constructor, then you have to have a constructor with at least the same signature as the original constructor and call MyBase.New(X). Your class D can have more parameters but one has to be an integer, which must be passed to the original constructor. If you think about it, it makes sense as you need to create an instance of both classes to create an instance of Class D.
-
If class D inherits From Class A and Class A has no paramterless constructor, then you have to have a constructor with at least the same signature as the original constructor and call MyBase.New(X). Your class D can have more parameters but one has to be an integer, which must be passed to the original constructor. If you think about it, it makes sense as you need to create an instance of both classes to create an instance of Class D.
-
The whole idea was to _not_ declare the constructor again. I agree that it makes sense to construct the base class object first, but I want the base class constructor to be used if I do not specify one in the derived class.
~ Soumya92
-
I have a MustInherit class A, something like
Public MustInherit Class A
Protected A as Integer Public Sub New(ByVal X As Integer) A=X End Sub Public MustOverride Sub B()
End Class
and I want the declared constructor to be used for any derived classes. However, in derived classes, like
Public Class D
Inherits APublic Sub B() A+=2 End Sub
End Class
VS gives me an error, saying that there is no constructor in A that can be called without any arguments. Is there any way I can inherit the constructor?
~ Soumya92
In other words, you want to be able to create a new "D" using the constructor defined in "A"? Short answer... You can't. Constructors don't inherit in .NET... You have to explicitly call them in a constructor in "D".
Public Sub New(ByVal X as Integer)
MyBase.New(X)
End SubIf you have a lot of these subclasses, then you might want to move the initialization code out of the constructor and into an
Initialize(X)
sub that you could call after instantiating it. Easy enough to set a boolean flag so it can only be called once.Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
In other words, you want to be able to create a new "D" using the constructor defined in "A"? Short answer... You can't. Constructors don't inherit in .NET... You have to explicitly call them in a constructor in "D".
Public Sub New(ByVal X as Integer)
MyBase.New(X)
End SubIf you have a lot of these subclasses, then you might want to move the initialization code out of the constructor and into an
Initialize(X)
sub that you could call after instantiating it. Easy enough to set a boolean flag so it can only be called once.Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
OK. Thanks a lot for your quick replies. We'll call the question resolved, then?
~ Soumya92
Yep, sorry it's not the answer you wanted.
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels)