Results 1 to 9 of 9

Thread: Having Trouble With Class Inheritance

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    26

    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

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    26

    Re: Having Trouble With Class Inheritance

    AAAHHHH!! I forgot the constructor in the base class.

  3. #3
    Hyperactive Member Runesmith's Avatar
    Join Date
    Oct 2008
    Posts
    399

    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
    Last edited by Runesmith; Sep 28th, 2009 at 03:25 AM. Reason: This guy is too smart
    Runesmith

    The key to getting the right answer is asking the right question

    I just realized: good health is merely the slowest possible rate at which one can die

  4. #4
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    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.

  5. #5
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Having Trouble With Class Inheritance

    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:
    1. Public Class MyClass
    2.  
    3. Public Sub SomeMethod
    4.  'some stuff here
    5. End Sub
    6.  
    7. End Class
    then I could do this even though I havent written the parameterless constructor:
    vb Code:
    1. 'In a form or wherever
    2.  
    3. Dim iMyClass As New MyClass
    4. iMyClass.SomeMethod()

    I know that doesnt affect the original question in this thread but just wanted to mention that
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  6. #6
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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.

  7. #7
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    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.

  8. #8
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Having Trouble With Class Inheritance

    I said that
    Unless you have created another constructor that accepts parameters
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  9. #9
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Having Trouble With Class Inheritance

    Quote Originally Posted by chris128 View Post
    I said that
    Didn't see that, my bad!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width