Re: Collections and updated Class properties
Form Class cont:
Public Sub NewValidate_ClearTextbox() Handles NewValidate.ClearTextbox
Dim ctlcontrol As Control 'Event handler from Validate Class
For Each ctlcontrol In Me.Controls
If TypeOf ctlcontrol Is TextBox Then
With ctlcontrol
.Text = ""
.Focus()
End With
End If
Next
End Sub
Private Sub btnOldCar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOldCar.Click
NewValidate = New Validate 'declare instance of Class object
intCount += 1 'index for array incremented
intCount2 += 1 'count for button click event
'incremented
'validity check
If NewValidate.ValidateData(txtMileage, "Mileage") Then
If NewValidate.ValidateData(txtGasLevel, "Gas Level") Then
If NewValidate.ValidateData(txtMaxGasLevel, "Max Gas Level") Then
If NewValidate.ValidateData(txtOilLevel, "Oil Level") Then
End If
End If
End If
End If
Dim strVIN As String 'variable declarations
Dim intMileage As Integer
Dim sngOil_Lev As Single
strVIN = txtVIN.Text 'assign textbox text value to
'variable
Try
Try 'try... catch to
'catch invalid cast exception and display messagebox
intMileage = CInt(txtMileage.Text)
sngOil_Lev = CSng(txtOilLevel.Text)
' Try
MyCar = New Car(strVIN, intMileage, True, counter)
ReDim Preserve strVINunique(intCount) 'redimension
'array so there is no limit on how many values it can
'hold; also preserve values already held
strVINunique(intCount) = strVIN 'assign VIN variable
'to array
'delclare
'instance of Car Class with parameters
MyCar.Add_Oil(sngOil_Lev)
MyCar.MaxGasLevel = CSng(txtMaxGasLevel.Text) 'assign
'textbox text property to Class property
MyCar.GasLevel() = CSng(txtGasLevel.Text)
' End Try
Catch esystem As NullReferenceException 'catch system exception and
'display message to user
MessageBox.Show("We are unable to create your New Car due to the " & _
"problem with the VIN number you entered. Sorry.")
End Try
Catch eSystem As InvalidCastException
MessageBox.Show("You must enter a numeric value above zero" _
& ControlChars.CrLf & " in all fields.")
End Try
If (MyCarCollection Is Nothing) Then
Try ' 'try.. catch
' MyBetterCar.Unique_VIN(strVINunique, strVIN, intCount, intCount2)
'call to Inherited Class to test for duplicate VINs
MyCarCollection = New CarCollection
Catch esystem As NullReferenceException 'catch system exception
MessageBox.Show("Car does not exist!")
End Try
End If
Try
Dim message As String = ""
MyCarCollection.Add(MyCar, intCount, intCount2, strVIN, message) 'add MyBetterCar object to Collection
If intCount <= 10 Then
MessageBox.Show("This is the " & message & " car you have added.")
End If
Me.collect_car_VIN()
Catch err As InvalidVINexception 'catch user defined exception
MessageBox.Show(err.Message) 'show error message stored in
'constuctor()
Catch err As InvalidVINPatternException 'catch user defined
'exception
MessageBox.Show(err.Message)
' Catch err As NonUniqueVINException 'catch user defined exception
Me.ClearText()
Catch err As NonUniqueVINException 'catch user defined
'exception
MessageBox.Show(err.Message) 'show error message
Me.ClearText()
Catch esystem As NullReferenceException 'catch system exception
MessageBox.Show("Please enter a VIN number that conforms to all of the " & _
ControlChars.CrLf & "requirements. Thank you.")
End Try
End Sub
Re: Collections and updated Class properties
Form Class cont;
Private Sub btnDisplayStandard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayStandard.Click
' Dim sendtxt As String
Try
MyCar.DisplayPropValues(SendTxt, counter, SendMile, _
MakeCurrentRun, SendGasLev, SendMaxGas, SendOil)
'call to Base Class method
Catch eSystem As NullReferenceException 'catch exception
'with message
MessageBox.Show("Please enter a numeric value greater than zero in all textboxes and" & _
ControlChars.CrLf & "be sure that you clicked the 'Create Standard...'" & _
ControlChars.CrLf & "button before clicking this 'Display Standard...' button" & _
ControlChars.CrLf & " and that you did not hit the 'Create Better...'button first")
End Try
End Sub
Private Sub btnPumpGasStandard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPumpGasStandard.Click
'PumpGas for Old Car
Dim sngGals As Single
MessageBox.Show("Please enter a number in the inputbox(coming up) " & _
ControlChars.CrLf & "and hit the 'Display Standard...' button to see the changes " _
& ControlChars.CrLf & "reflected.", "Instructions", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
Try 'try... catch to catch exception
sngGals = CSng(InputBox("Please enter the number of gallons of gas to pump", _
"How many gallons?")) 'inputbox
Catch eCast As InvalidCastException
MessageBox.Show("You have pressed 'Cancel' ", "Cancel chosen", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
Finally
Try
Try
MyCar.PumpGas(sngGals)
Catch err As AmountPumpedNegativeException
MessageBox.Show(err.Message & ControlChars.CrLf & _
"The source of the problem is: " & err.Source)
Dim bCheck As Boolean = True
If bCheck = True Then Me.ClearText()
End Try
' MyCar.PumpGas(sngGals) 'call to MyCar Class method
Catch eSystem As NullReferenceException
MessageBox.Show("You must enter values in all the textboxes and" & _
ControlChars.CrLf & " click the 'Create' button before using this control")
End Try
End Try
End Sub
Private Sub btnPumpGasBetter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPumpGasBetter.Click
'PumpGas for BetterCar
Dim sngGals As Single
MessageBox.Show("Please enter a number in the inputbox(coming up) " & _
ControlChars.CrLf & "and hit the 'Display Better....' button to see the changes " _
& ControlChars.CrLf & "reflected.", "Instructions", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
Try 'try... catch to catch exception
sngGals = CSng(InputBox("Please enter the number of gallons of gas to pump", _
"How many gallons?")) 'inputbox
Catch eCast As InvalidCastException
MessageBox.Show("You have pressed 'Cancel' ", "Cancel chosen", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
Finally
Try 'try.. catch
Try
MyBetterCar.PumpGas(sngGals)
Catch err As AmountPumpedNegativeException 'catch user
'defined exception
MessageBox.Show(err.Message & ControlChars.CrLf & _
"The source of the problem is: " & err.Source)
Dim bCheck As Boolean = True
If bCheck = True Then Me.ClearText()
End Try
Catch eSystem As NullReferenceException
MessageBox.Show("You must enter values in all the textboxes and" & _
ControlChars.CrLf & " click the 'Create' button before using this control")
End Try
End Try
End Sub
Private Sub btnCheckOilBetter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckOilBetter.Click
Dim OilLevel As Single
Try 'try.. catch
OilLevel = CSng(txtOilLevel.Text)
MyBetterCar.Check_Oil(OilLevel) 'call to Derived Class method
Catch eSystem As InvalidCastException
MessageBox.Show("You must enter values in all the textboxes and " _
& ControlChars.CrLf & "click the 'Create' button before using this control")
End Try
End Sub
Public Sub ServLightOn() 'sub procedure to turn on Service light
Dim sLabelText As String = "Light On"
Me.lblService.Show()
Me.lblService.BackColor = System.Drawing.Color.Red()
Me.lblService.Text = sLabelText
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'form load procedure
Me.txtVIN.Select()
Me.lblService.Hide()
End Sub
Public Sub ServLightOff() 'sub to turn service light off
Dim sLabelText As String = "Light Off"
Me.lblService.Show()
Me.lblService.BackColor = System.Drawing.Color.Gray
Me.lblService.ForeColor = System.Drawing.Color.White
Me.lblService.Text = sLabelText
End Sub
Private Sub btnMakeCurrent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMakeCurrent.Click
'click event to call MakeCurrent procedure
If Me.MakeCurrent() = True Then
MakeCurrentRun = True
End If
End Sub
Private Sub collect_car_VIN() 'sub procedure to add VINs to ComboBox
If counter >= 0 Then
cboCars.Items.Add(MyCarCollection.Item(counter).VIN)
counter += 1
End If
End Sub
Private Sub btnDeleteCar_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnDeleteCar.Click
'event that uses Remove methos of CollectionBase
If cboCars.SelectedIndex <> (-1) Then
If MessageBox.Show("Are you sure you want to delete the Car?", "Delete?", _
MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Try
MyCarCollection.RemoveAt(cboCars.SelectedIndex)
Me.ClearText()
cboCars.Items.RemoveAt(cboCars.SelectedIndex)
cboCars.Refresh()
If cboCars.Items.Count = 0 Then
cboCars.Text = ""
MessageBox.Show("You have now deleted all of the Cars in the Collection.", _
"Cars deleted")
End If
Catch ex As System.InvalidCastException
MessageBox.Show("invalid cast")
Catch ex As Exception
End Try
End If
Else
MessageBox.Show("Please select a VIN and Car to delete", "Choose a VIN", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Public Function MakeCurrent() As Boolean 'procedure to select Car from ComboBox
' selectedCar = New Car(strVIN, intMileage, bValue, counter)
If Me.cboCars.SelectedIndex = -1 Then
Me.selectedCar = Nothing
Me.ClearText()
Else
Me.selectedCar = Me.MyCarCollection.Item(Me.cboCars.SelectedIndex)
Me.txtVIN.Text = Me.selectedCar.VIN
Me.txtMileage.Text = CStr(Me.selectedCar.Mileage)
Me.txtGasLevel.Text = CStr(Me.selectedCar.GasLevel)
Me.txtMaxGasLevel.Text = CStr(Me.selectedCar.MaxGasLevel)
Me.txtOilLevel.Text = CStr(Me.selectedCar.Oil)
End If
' txtVIN.Text = MyCarCollection.Item(cboCars.SelectedIndex).VIN.ToString
' txtMileage.Text = MyCarCollection.Item(cboCars.SelectedIndex).Mileage.ToString
' txtGasLevel.Text = MyCarCollection.Item(cboCars.SelectedIndex).GasLevel.ToString
' txtMaxGasLevel.Text = MyCarCollection.Item(cboCars.SelectedIndex).MaxGasLevel.ToString
' txtOilLevel.Text = MyCarCollection.Item(cboCars.SelectedIndex).Oil.ToString
' cboCars.Refresh()
SendTxt = txtVIN.Text 'assign values to Global variables that are passed
'DisplayPropertyValues method
SendMile = txtMileage.Text
SendGasLev = txtGasLevel.Text
SendMaxGas = txtMaxGasLevel.Text
SendOil = txtOilLevel.Text
Return True
End Function
Public Sub MyBetterCar_RefreshCurrent2() Handles MyBetterCar.RefreshCurrent2
Me.MakeCurrent()
End Sub
Public Sub MyCar_RefreshCurrent() Handles MyCar.RefreshCurrent
Me.MakeCurrent()
End Sub
End Class
Re: Collections and updated Class properties
CarCollection:
Imports System.Text.RegularExpressions 'imports class
Public Class CarCollection 'CarCollection Class
Inherits System.Collections.CollectionBase 'inherits CollectionBase
Public Event CloseProgram()
Public Sub Add(ByVal aCar As Car, ByVal intcount2 As Integer, _
ByVal intcount As Integer, ByVal vin_num As String, _
ByRef message As String) 'Add method
Me.NumCharVin(vin_num)
Me.VINFormat(vin_num)
Me.VinDup(vin_num, intcount2, intcount) 'call to procedure that tests for
' duplicates()
Try
Me.ExceedMaxCars()
List.Add(aCar)
Me.MaxCars(message)
Dim count As String
count = CStr(list.Count)
Select Case count
Case "1"
message = count & "st"
Case "2"
message = count & "nd"
Case "3"
message = count & "rd"
Case "4"
message = count & "th"
Case "5"
message = count & "th"
Case "6"
message = count & "th"
Case "7"
message = count & "th"
Case "8"
message = count & "th"
Case "9"
message = count & "th"
End Select
Catch err As MaxCarsException
MessageBox.Show(err.Message)
Catch err As ExceededCarsException
MessageBox.Show(err.Message)
End Try
If Me.List.Count <= 9 Then
MessageBox.Show("You have successfully added a new Car.")
End If
End Sub
Public Sub Remove(ByVal index As Integer) 'remove method
If index > Count - 1 Or index < 0 Then
MessageBox.Show("There seems to be no existing car!", _
"No Car", MessageBoxButtons.OKCancel, _
MessageBoxIcon.Exclamation)
System.Windows.Forms.MessageBox.Show("Index not valid!")
Else
List.RemoveAt(index)
End If
End Sub
Public ReadOnly Property Item(ByVal index As Integer) As Car 'Item property
Get
Try
Return CType(List.Item(index), Car)
Catch syserr As ArgumentOutOfRangeException
MessageBox.Show("Sorry, but you can not add another car at this point.")
Catch syserr As NullReferenceException
MessageBox.Show("Sorry for the incovienience. The program will now close.")
End Try
End Get
End Property
Public Sub VinDup(ByVal vin As String, ByVal intcount2 As Integer, _
ByVal intcount As Integer) 'procedure to test for duplicate VINs
If intcount2 > 1 Then
For intcount = 0 To Count - 1
If Me.Item(intcount).VIN = vin Then
Throw New NonUniqueVINException(vin) 'exception thrown
End If
Next
End If
End Sub
Public Sub NumCharVin(ByVal vin As String)
If Not vin.Length.Equals(17) Then 'test for length of VIN
Throw New InvalidVINexception(vin) 'throw exception
End If
End Sub
Public Sub VINFormat(ByVal vin As String)
Dim objRegEx As New Regex("[JS 1-6 8-9]{1}[NT]{1}[123457 ABDEGKLMX]{1}" & _
"[^EINOQRUVW a-z]{1}[^CIJOQVWXZ 0-3 5-9]{1}[1-9]{1}[^459 aA-zZ]{1}" & _
"[^25689 GHILMOQSWXYZ a-z]{1}[1-9]{1}[0-9]{1}[0-9 CMSUZ]{1}[0-9]{6}")
'creates new regular expression object
If Not objRegEx.IsMatch(vin) Then 'uses regular expression
'property to test for match
Throw New InvalidVINPatternException(vin)
End If
End Sub
Public Sub MaxCars(ByRef message As String)
If (Me.List.Count) > 9 Then
Dim count As String
count = CStr(list.Count)
If count = "10" Then
message = count & "th"
Throw New MaxCarsException
End If
End If
End Sub
Public Function ExceedMaxCars() As Boolean
If Me.List.Count = 10 Then
Throw New ExceededCarsException
End If
Return True
End Function
End Class
Re: Collections and updated Class properties
Here is the Car Class where I set the properties for the Car.It includes the Drive Method. There is also a Better Car Class that inherits from Car. I will post it if you like.Public Class Car
Public Event OilGood() 'declare events
Public Event ClearMiles()
Public Event LowOil()
Public Event ClearTextbox1()
Public Event GasLow()
Public Event RemoveCar()
Protected blnServLight As Boolean
Protected sVIN As String 'private instance variables
Protected sOil As Single
Protected intMileage As Integer 'protected instance variables
'so derived class has access
Protected sngGasLevel As Single
Private sngMaxGasLevel As Single
Public ReadOnly Property VIN() As String 'VIN property procedure
'read only
Get 'accessor that is invoked when
'a request to retrieve the property value is made
Return Me.sVIN
End Get
End Property
Public ReadOnly Property Mileage() As Integer 'Mileage property procedure
Get
Return Me.intMileage
End Get
End Property
Public Property GasLevel() As Single 'GasLevel property procedure
Get
Return Me.sngGasLevel 'return value like a function
End Get
Set(ByVal Value As Single)
Me.sngGasLevel = Value
Dim ctlControl As Control 'declare variable as Control type
If Value < 0 Then 'check for proper value
MessageBox.Show("Please enter a positive value for Gas Level!" _
& ControlChars.CrLf & "Gas level is below '0'", "Negative Gas Level!", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
RaiseEvent ClearTextbox1() 'event raised
ElseIf Value >= 0 And Value < 2 Then 'conditional clause
RaiseEvent GasLow() 'event raised
' Me.sngGasLevel = Value
ElseIf Value > Me.MaxGasLevel Then
MessageBox.Show("Tank is overfilled! Exceeded Max" & _
" " & "gas level!", "Too much gas!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
RaiseEvent ClearTextbox1() 'event raised
Else
Me.sngGasLevel = Value 'assings Value parameter to
'private instance variable
End If
End Set
End Property
Public Property MaxGasLevel() As Single 'MaxGasLevel property procedure
Get
Return Me.sngMaxGasLevel
End Get
Set(ByVal Value As Single)
Me.sngMaxGasLevel = Value
End Set
End Property
Public ReadOnly Property Oil() As Single
'read only property procedure
Get
Return Me.sOil
End Get
End Property
Public Overridable ReadOnly Property ServiceLight() As Boolean
Get 'property procedure
Return blnServLight
End Get
End Property
Sub New(ByVal VIN_Num As String, ByVal Mileage_Go As Integer, _
ByVal service As Boolean, ByRef count As Integer)
'Constructor
Me.sVIN = VIN_Num 'assign parameters passed to Get of Property
'procedures
Me.intMileage = Mileage_Go
End Sub
Public Overridable Function Drive(ByVal decMiles As Decimal) As Boolean
'Drive Class Method
'Overriable
If decMiles >= 0 Then
MessageBox.Show("The current mileage on the car has been increased " & _
ControlChars.CrLf & "by the amount you entered in the textbox and " & _
ControlChars.CrLf & "the gas level has been decrease by 10% of the " & _
ControlChars.CrLf & "total mileage. To see these changes reflected please " & _
ControlChars.CrLf & " click on the Display button.")
Me.intMileage += CInt(decMiles)
Me.GasLevel -= CSng((0.1 * Mileage))
Else
Dim bCheck As Boolean
bCheck = True
Throw New NegativeAmountDrivenException(decMiles, bCheck)
End If
End Function
Public Overridable Sub PumpGas(ByVal sngNumGal As Single) 'Class Method
If Me.GasLevel < 0 Then 'checks for proper value entered
MessageBox.Show("You need to add gas ", "No Gas in tank! ", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
ElseIf Me.GasLevel > Me.MaxGasLevel Then
MessageBox.Show("Too much gas in tank!", "Overmax!", MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation)
Else
If sngNumGal >= 0 Then
Me.GasLevel += sngNumGal 'computation using parameter passed
Else
Throw New AmountPumpedNegativeException(sngNumGal)
End If
End If
End Sub
Public Overridable Sub DisplayPropValues(ByVal ReceiveTxt As String, _
ByVal counter As Integer, ByVal ReceiveMile As String, _
ByVal MakeCurrentRun As Boolean, ByVal ReceivedGasLev As String, _
ByVal ReceiveMaxGas As String, ByVal ReceiveOil As String)
'Class method overridable
Dim bValue As Boolean
Dim result As DialogResult
' Dim counter As Integer
If Not MakeCurrentRun Then
If MessageBox.Show("VIN number is: " & Me.VIN & _
ControlChars.CrLf & "Mileage is: " & Me.Mileage _
& " Miles" & ControlChars.CrLf & "Gas Level is: " & _
FormatNumber(Me.GasLevel) & " Gallons" & ControlChars.CrLf & _
"Max Gas Level is: " & FormatNumber(Me.MaxGasLevel) & " Gallons" & ControlChars.CrLf & _
"Oil level is: " & Me.sOil & " Quarts" & ControlChars.CrLf & _
"Do you want to create a new Car?", "New Car?", MessageBoxButtons.YesNo, _
MessageBoxIcon.Question) = result.Yes Then
Dim mycar As Car 'new instance of Car Class
mycar = New Car(sVIN, intMileage, bValue, counter)
RaiseEvent ClearTextbox1() 'event raised
Else
RaiseEvent ClearMiles()
End If
Else
If MessageBox.Show("VIN number is: " & ReceiveTxt & _
ControlChars.CrLf & "Mileage is: " & ReceiveMile _
& " Miles" & ControlChars.CrLf & "Gas Level is: " & _
ReceivedGasLev & " Gallons" & ControlChars.CrLf & _
"Max Gas Level is: " & ReceiveMaxGas & " Gallons" & ControlChars.CrLf & _
"Oil level is: " & Me.sOil & " Quarts" & ControlChars.CrLf & _
"Do you want to create a new Car?", "New Car?", MessageBoxButtons.YesNo, _
MessageBoxIcon.Question) = result.Yes Then
Dim mycar As Car 'new instance of Car Class
mycar = New Car(sVIN, intMileage, bValue, counter)
RaiseEvent ClearTextbox1() 'event raised
Else
RaiseEvent ClearMiles()
End If
End If
End Sub
Public Sub Check_Oil(ByVal sngOilLev As Single) 'Class method
sOil = sngOilLev 'parameter assigned to private instance
'variable
If Me.sOil <= 2 Then
RaiseEvent LowOil() 'event raised
Else
RaiseEvent OilGood() 'event raised
End If
End Sub
Public Sub Add_Oil(ByVal sngOil As Single) 'Class method to
'assign parameter to read only property using private instance
'variable
sOil = sngOil
End Sub
Public Overridable Sub Unique_VIN(ByVal vin_unique() As String, _
ByVal vin As String, ByVal intcount As Integer, ByVal intcount2 As Integer) 'ByRef vin_unique() As String, _
'overridable sub that tests for duplicate VINs (will not work for Old Car
'VIN duplicate check.. just serves so MyBetterCar overrided method works)
If intcount2 > 1 Then 'tests for number of times Click Event
'that creates Better Car has been run
Do While intcount > 0 'do.. while loop to test for
'duplicate VINs
intcount -= 1
If vin_unique(intcount) = vin Then
intcount -= 1
Throw New NonUniqueVINException(vin) 'exception throw
End If
Loop
End If
End Sub
End Class