Results 1 to 4 of 4

Thread: Please help, VB.net I need help to understand I have most of the problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2016
    Posts
    3

    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

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    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

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    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?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,348

    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:
    1. Public Class Car
    2.  
    3.     Dim mVinNumber As String
    4.     Dim mGasLevel As Single
    5.     Dim mMileage As Single
    6.     Dim mMpg As Single
    7.  
    8.  
    9.  
    10.     Public Property VinNumber() As String
    11.         Get
    12.             Return mVinNumber
    13.         End Get
    14.         Set(ByVal value As String)
    15.             mVinNumber = value
    16.         End Set
    17.     End Property
    18.  
    19.  
    20.     Public Property GasLevel() As Single
    21.         Get
    22.             Return mGasLevel
    23.         End Get
    24.         Set(ByVal value As Single)
    25.             If value > 0 then
    26.             mGasLevel = value
    27.             End If
    28.         End Set
    29.     End Property
    30.  
    31.  
    32.     Public Property Mileage() As Single
    33.         Get
    34.             Return mMileage
    35.         End Get
    36.         Set(ByVal value As Single)
    37.             If value > 0 then
    38.             mMileage = value
    39.             End If
    40.         End Set
    41.     End Property
    42.  
    43.     Public Property Mpg() As Single
    44.         Get
    45.             Return mMpg
    46.         End Get
    47.         Set(ByVal value As Single)
    48.            If value > 0 then
    49.             mMpg = value
    50.             End If
    51.         End Set
    52.     End Property
    53.  
    54.    Public Sub Drive(ByVal Miles As Single)
    55.         Dim Range As Single = mMpg * mGasLevel  'current capacity
    56.         If Miles > Range Then
    57.         Messagebox.show("not enought gas to drive that far")
    58.         ElseIf Miles > 0 Then
    59.         Mileage += Miles  'increase mileage
    60.        
    61.         GasLevel -= (Miles / MPG)  'decrease gas
    62.         Else
    63.         Messagebox.show("Enter a positive amount of miles to drive")
    64.         End If
    65.     End Sub
    66.  
    67.     Public Sub PumpGas(ByVal Gallons As Single)
    68.         If Gallons > 0 Then
    69.         GasLevel += Gallons   'note we are not checking for max tank capacity
    70.         Else
    71.         Messagebox.show("Please enter a postive number for Gallons")
    72.     End Sub
    73.  
    74. End Class
    75.  
    76. Public Class fmDrive
    77.  
    78.     Dim myCar As Car
    79.  
    80.     Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
    81.  
    82.         'not required but a nice touch - check if user wants to throw away old car
    83.         Dim Proceed As Boolean = True
    84.         If mycar <> Nothing Then
    85.        
    86.         If Messagebox.show("your current car will be destroyed. Continue?","New Car",Messageboxbuttons.YesNo) = DialogResult.No Then
    87.         Proceed = False
    88.         End If
    89.         End If
    90.        
    91.         If Proceed Then
    92.    
    93.         myCar = New Car(CStr(txtVIN.Text), CSng(txtMileage.Text), CSng(txtGasLevel.Text), CSng(txtMPG.Text))
    94.         UpdateDisplay()
    95.         End If
    96.        
    97.        
    98.  
    99.     End Sub
    100.  
    101.     Private Sub btnDrive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDrive.Click
    102.         If myCar Is Nothing Then
    103.             MessageBox.Show("You must create a car first before you drive.", "No Car Has Been Created Yet")
    104.         Else
    105.             myCar.Drive(CSng(InputBox("How many miles to drive?")))
    106.             UpdateDisplay()
    107.         End If
    108.        
    109.     End Sub
    110.  
    111.     Private Sub btnPump_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPump.Click
    112.         If myCar Is Nothing Then
    113.             MessageBox.Show("You must create a car first before you pump gas.", "No Car Has Been Created")
    114.         Else
    115.             myCar.PumpGas(CSng(InputBox("How much gas to pump into the car?")))
    116.             UpdateDisplay()
    117.         End If
    118.  
    119.     End Sub
    120.  
    121.     Private Sub UpdateDisplay()
    122.         txtGasLevel.Text = myCar.GasLevel.ToString()
    123.         txtMileage.Text = myCar.Mileage.ToString()
    124.         txtMPG.Text = myCar.MPG.ToString()
    125.         txtVIN.Text = myCar.VIN
    126.        
    127.     End Sub
    128.  
    129.     Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
    130.         If mycar <> Nothing then
    131.        
    132.         myCar = Nothing 'kill a car
    133.  
    134.         txtGasLevel.Clear()
    135.         txtMPG.Clear()
    136.         txtMileage.Clear()
    137.         txtVIN.Clear()
    138.         Else
    139.         Messagebox.show("Nothing to kill")
    140.        End If
    141.        
    142.  
    143.     End Sub
    144.  
    145.    
    146.  
    147.     Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    148.         Me.Close()
    149.     End Sub
    150. 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
  •  



Click Here to Expand Forum to Full Width