Results 1 to 3 of 3

Thread: [2005] Class Inheritance

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    59

    [2005] Class Inheritance

    I have the following class definition

    Code:
    Public Class Car
        Public ReadOnly MaxSpeed As Integer
        Private CurrSpeed As Integer
    
        Public Sub New()
    
        End Sub
        Public Sub New(ByVal max As Integer)
            MaxSpeed = 55
        End Sub
    
        Public Property Speed() As Integer
            Get
                Return CurrSpeed
            End Get
            Set(ByVal value As Integer)
                CurrSpeed += value
                If CurrSpeed > MaxSpeed Then
                    CurrSpeed = MaxSpeed
                End If
            End Set
        End Property
    End Class
    
    Public Class MiniVan
        Inherits Car
    
    End Class
    In the main code I access it as follows

    Code:
    Module Module1
    
        Sub Main()
            Dim myCar As New Car(80)
            myCar.Speed = 50
            Console.WriteLine("My car is going {0} MPH", myCar.Speed)
            Console.WriteLine()
    
            Dim myVan As New MiniVan()
            myVan.Speed = 10
            Console.WriteLine("My van is going {0} MPH", myVan.Speed)
            Console.WriteLine()
            Console.ReadLine()
        End Sub
    
    End Module
    When I run the Console App, I get the following output but I believe I should be getting - My van is going 10 mph.

    My car is going 50 MPH

    My van is going 0 MPH

    Can somebody please explain, where I am going wrong?

    Thanks
    Edit/Delete Message

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: [2005] Class Inheritance

    At a glance...

    When you declare myVan you aren't passing in a maximum speed unlike when you declare myCar, so it is defaulting max speed to zero.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Class Inheritance

    If you had used the debugger you'd have seen exactly what keystone_paul said. If you'd placed a breakpoint on the Set method of the Speed property you'd have seen that execution enters the If block, because CurrSpeed is greater than MaxSpeed, and sets CurrSpeed to the value of MaxSpeed, which is zero.

    The debugger exists so you can debug your code, so you should make use of it. The first tools you need to use are breakpoints (F9), which halt execution at a specific line of code, and then Step Over (F10) and Step Into (F11), which let you step through your code one line at a time. At each step you can mouse over variables and properties to determine their value, use the Autos, Locals, Watch and Immediate windows to evaluate variables, properties, methods and expressions and use the Command window to execute code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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