Having Trouble With Class Inheritance
I am learning about class inheritance and need some help with the code below. I have two classes:
Product and Book.
In my Book class I have a line:
MyBase.new(code, description, price)
I am getting an error on that line stating:
Too many arguments to Public Sub New()
Can someone tell me why I am getting such an error?
Thanks
Code:
Public Class Product
Private m_Code As String
Private m_Description As String
Private m_Price As Decimal
Public Property Code() As String
Get
Return m_Code
End Get
Set(ByVal value As String)
m_Code = value
End Set
End Property
Public Property Description() As String
Get
Return m_Description
End Get
Set(ByVal value As String)
m_Description = value
End Set
End Property
Public Property Price() As Decimal
Get
Return m_Price
End Get
Set(ByVal value As Decimal)
m_Price = value
End Set
End Property
Public Overridable Function GetDisplayText(ByVal sep As String) As String
Return Code & sep & Description & sep & FormatCurrency(Price).ToString
End Function
End Class
Public Class Book
Inherits Product
Public Author As String
Public Sub New(ByVal code As String, ByVal description As String, ByVal author As String, ByVal price As Decimal)
MyBase.new(code, description, price)
Me.Author = author
End Sub
End Class
Re: Having Trouble With Class Inheritance
AAAHHHH!! I forgot the constructor in the base class.
Re: Having Trouble With Class Inheritance
Where is your New() for the class Product?
In the line MyBase.new() in Book.new(), you are passing three arguments to product.new(). If Product.new() is not defined with three parameters, you will get this error.
EDIT: haha, yes you got it while I was typing my reply :)
Re: Having Trouble With Class Inheritance
MyBase.New calls Sub New of the base class, the one from which you inherit. In this case, the base class is Product.
But Product doesn't have a Sub New yet, let alone one that takes those three arguments. So give it one!
regards, BB
edit: Oops, three at once. Well that happens with the easy questions, doesn't it.
Re: Having Trouble With Class Inheritance
Quote:
But Product doesn't have a Sub New yet
You dont need to create the default constructor though do you? Unless you have created another constructor that accepts parameters.
So I could do this:
vb Code:
Public Class MyClass
Public Sub SomeMethod
'some stuff here
End Sub
End Class
then I could do this even though I havent written the parameterless constructor:
vb Code:
'In a form or wherever
Dim iMyClass As New MyClass
iMyClass.SomeMethod()
I know that doesnt affect the original question in this thread but just wanted to mention that :)
Re: Having Trouble With Class Inheritance
No, you don't need to specify a constructor if you don't use it for anything. If you want something to happen in the constructor, or if you want the constructor to take parameters, or if you want to hide it (make it Private for instance), only then you need to create the constructor code.
It seems the question is resolved already. Please mark the thread as resolved from the Thread Tools if it is. :)
Re: Having Trouble With Class Inheritance
You need to create the parameter-less constructor if you want both parametrized and parameter-less constructor overloads however.
Re: Having Trouble With Class Inheritance
I said that :)
Quote:
Unless you have created another constructor that accepts parameters
Re: Having Trouble With Class Inheritance
Quote:
Originally Posted by
chris128
I said that :)
Didn't see that, my bad!