|
-
Dec 6th, 2008, 07:24 AM
#1
Thread Starter
Member
[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
-
Dec 6th, 2008, 07:27 AM
#2
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.
-
Dec 6th, 2008, 08:45 AM
#3
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|