-
Apr 6th, 2016, 09:32 PM
#1
Thread Starter
New Member
Please help, VB.net I need help to understand I have most of the problem
I am having trouble with excercise 1 and 2
1. Greencar will suppress the parent's Gaslow event so that objects based on GreenCar will not be able to respond to the Gaslow as is. Instead, when Gaslow fires, GreenCar will raise its own GasLow - you may use a different name- But only if the gas is at 2 or less gallons.
2. On the client side, divide the form in two areas, one for Car and another for GreenCar. Make sure each has the needed controls to enter each car's data, with two sets of buttons to Drive, and Pump Gas. However, since both sets of Drive and Pump Gas buttons will do the same actions; handle these events in one handler per button. For example handle Car and GreenCar's Drive click events in one handler, and do the same for the Pump Gas buttons.
Public Class Car
Dim mVinNumber As String
Dim mGasLevel As Single
Dim mMileage As Single
Dim mMpg As Single
Public Property VinNumber() As String
Get
Return mVinNumber
End Get
Set(ByVal value As String)
mVinNumber = value
End Set
End Property
Public Property GasLevel() As Single
Get
Return mGasLevel
End Get
Set(ByVal value As Single)
If value > 0 then
mGasLevel = value
End If
End Set
End Property
Public Property Mileage() As Single
Get
Return mMileage
End Get
Set(ByVal value As Single)
If value > 0 then
mMileage = value
End If
End Set
End Property
Public Property Mpg() As Single
Get
Return mMpg
End Get
Set(ByVal value As Single)
If value > 0 then
mMpg = value
End If
End Set
End Property
Public Sub Drive(ByVal Miles As Single)
Dim Range As Single = mMpg * mGasLevel 'current capacity
If Miles > Range Then
Messagebox.show("not enought gas to drive that far")
ElseIf Miles > 0 Then
Mileage += Miles 'increase mileage
GasLevel -= (Miles / MPG) 'decrease gas
Else
Messagebox.show("Enter a positive amount of miles to drive")
End If
End Sub
Public Sub PumpGas(ByVal Gallons As Single)
If Gallons > 0 Then
GasLevel += Gallons 'note we are not checking for max tank capacity
Else
Messagebox.show("Please enter a postive number for Gallons")
End Sub
End Class
----------------------------------------------------------------------------------------------------------------
Public Class fmDrive
Dim myCar As Car
Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
'not required but a nice touch - check if user wants to throw away old car
Dim Proceed As Boolean = True
If mycar <> Nothing Then
If Messagebox.show("your current car will be destroyed. Continue?","New Car",Messageboxbuttons.YesNo) = DialogResult.No Then
Proceed = False
End If
End If
If Proceed Then
myCar = New Car(CStr(txtVIN.Text), CSng(txtMileage.Text), CSng(txtGasLevel.Text), CSng(txtMPG.Text))
UpdateDisplay()
End If
End Sub
Private Sub btnDrive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDrive.Click
If myCar Is Nothing Then
MessageBox.Show("You must create a car first before you drive.", "No Car Has Been Created Yet")
Else
myCar.Drive(CSng(InputBox("How many miles to drive?")))
UpdateDisplay()
End If
End Sub
Private Sub btnPump_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPump.Click
If myCar Is Nothing Then
MessageBox.Show("You must create a car first before you pump gas.", "No Car Has Been Created")
Else
myCar.PumpGas(CSng(InputBox("How much gas to pump into the car?")))
UpdateDisplay()
End If
End Sub
Private Sub UpdateDisplay()
txtGasLevel.Text = myCar.GasLevel.ToString()
txtMileage.Text = myCar.Mileage.ToString()
txtMPG.Text = myCar.MPG.ToString()
txtVIN.Text = myCar.VIN
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
If mycar <> Nothing then
myCar = Nothing 'kill a car
txtGasLevel.Clear()
txtMPG.Clear()
txtMileage.Clear()
txtVIN.Clear()
Else
Messagebox.show("Nothing to kill")
End If
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
-
Apr 6th, 2016, 09:58 PM
#2
Re: Please help, VB.net I need help to understand I have most of the problem
Where's the GasLow Event???
You need to declare a Public Event in your Car class, raise that event at the appropriate time, then handle that event in fmDrive
https://msdn.microsoft.com/en-us/library/ms973905.aspx
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 6th, 2016, 10:03 PM
#3
Re: Please help, VB.net I need help to understand I have most of the problem
It also says you should have a GreenCar class, so i'm not sure how you're supposed to suppress the Event or even that an Event is necessary if you follow the specifications. Events are used when you create instances of classes, such as your myCar instance. Maybe you need a GreenCar instance too?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 6th, 2016, 10:21 PM
#4
Re: Please help, VB.net I need help to understand I have most of the problem
Firstly, please use formatting tags when posting code snippets for readability. Here's how your code should look:
vb.net Code:
Public Class Car Dim mVinNumber As String Dim mGasLevel As Single Dim mMileage As Single Dim mMpg As Single Public Property VinNumber() As String Get Return mVinNumber End Get Set(ByVal value As String) mVinNumber = value End Set End Property Public Property GasLevel() As Single Get Return mGasLevel End Get Set(ByVal value As Single) If value > 0 then mGasLevel = value End If End Set End Property Public Property Mileage() As Single Get Return mMileage End Get Set(ByVal value As Single) If value > 0 then mMileage = value End If End Set End Property Public Property Mpg() As Single Get Return mMpg End Get Set(ByVal value As Single) If value > 0 then mMpg = value End If End Set End Property Public Sub Drive(ByVal Miles As Single) Dim Range As Single = mMpg * mGasLevel 'current capacity If Miles > Range Then Messagebox.show("not enought gas to drive that far") ElseIf Miles > 0 Then Mileage += Miles 'increase mileage GasLevel -= (Miles / MPG) 'decrease gas Else Messagebox.show("Enter a positive amount of miles to drive") End If End Sub Public Sub PumpGas(ByVal Gallons As Single) If Gallons > 0 Then GasLevel += Gallons 'note we are not checking for max tank capacity Else Messagebox.show("Please enter a postive number for Gallons") End Sub End Class Public Class fmDrive Dim myCar As Car Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click 'not required but a nice touch - check if user wants to throw away old car Dim Proceed As Boolean = True If mycar <> Nothing Then If Messagebox.show("your current car will be destroyed. Continue?","New Car",Messageboxbuttons.YesNo) = DialogResult.No Then Proceed = False End If End If If Proceed Then myCar = New Car(CStr(txtVIN.Text), CSng(txtMileage.Text), CSng(txtGasLevel.Text), CSng(txtMPG.Text)) UpdateDisplay() End If End Sub Private Sub btnDrive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDrive.Click If myCar Is Nothing Then MessageBox.Show("You must create a car first before you drive.", "No Car Has Been Created Yet") Else myCar.Drive(CSng(InputBox("How many miles to drive?"))) UpdateDisplay() End If End Sub Private Sub btnPump_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPump.Click If myCar Is Nothing Then MessageBox.Show("You must create a car first before you pump gas.", "No Car Has Been Created") Else myCar.PumpGas(CSng(InputBox("How much gas to pump into the car?"))) UpdateDisplay() End If End Sub Private Sub UpdateDisplay() txtGasLevel.Text = myCar.GasLevel.ToString() txtMileage.Text = myCar.Mileage.ToString() txtMPG.Text = myCar.MPG.ToString() txtVIN.Text = myCar.VIN End Sub Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click If mycar <> Nothing then myCar = Nothing 'kill a car txtGasLevel.Clear() txtMPG.Clear() txtMileage.Clear() txtVIN.Clear() Else Messagebox.show("Nothing to kill") End If End Sub Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click Me.Close() End Sub End Class
As for the issue, I would suggest that you follow the Blog link in my signature below and check out my post on Custom events. It will show you that you should declare an OnSomeEvent method to raise the SomeEvent event. You can then suppress that event in a derived class by overriding that method and not calling the base method. If you need to raise a different event instead, you can do that in the overridden method.
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
|