私はこのコードを持っていました:
Private Sub carButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles carButton.Click
fTrafficSurveyA.incrementCount("Car")
Call updateView()
End Sub
Private Sub bicycleButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bicycleButton.Click
fTrafficSurveyA.incrementCount("Bicycle")
Call updateView()
End Sub
Private Sub lorryButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lorryButton.Click
fTrafficSurveyA.incrementCount("Lorry")
Call updateView()
End Sub
私はそれを次のように変更しました:
Private Sub carButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles carButton.Click
ButtonClickCode(CType(sender, Button))
End Sub
Private Sub bicycleButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bicycleButton.Click
ButtonClickCode(CType(sender, Button))
End Sub
Private Sub lorryButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lorryButton.Click
ButtonClickCode(CType(sender, Button))
End Sub
Private Sub ButtonClickCode(ByVal mySender As Button)
Dim incrementVehicle As String
Select Case mySender.Name
Case "carButton"
incrementVehicle = "Car"
Case "bicycleButton"
incrementVehicle = "Bicycle"
Case "lorryButton"
incrementVehicle = "Lorry"
Case Else
incrementVehicle = ""
End Select
fTrafficSurveyA.incrementCount(incrementVehicle)
Call updateView()
End Sub
新しいコードには、より多くのコード行があります。
新しいバージョンの方が良い習慣ですか、それとも私が従うべき3番目のパターンがありますか?
編集
Guffaの回答と他のコメントを使用して、基になるTrafficSurveyA
モデルを列挙型を含むように変更し、その機能incrementCount
をそのように変更しました....この列挙型は正しい場所にありますか?
Enum vehicleType
Car
Lorry
Bicycle
End Enum
Public Sub incrementCount(ByVal vehicle As vehicleType)
' Preconditions: none
' Postconditions: If vehicle is "Car", "Bicycle" or "Lorry" then 1 is added
' to the corresponding count. Otherwise nothing is done.
Select Case vehicle
Case vehicleType.Car : fCars = fCars + 1
Case vehicleType.Bicycle : fBicycles = fBicycles + 1
Case vehicleType.Lorry : fLorries = fLorries + 1
Case Else 'do nothing
End Select
End Sub
インターフェイスコードは次のようになりました。
Private Sub carButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles carButton.Click
ButtonClickCode(TrafficSurveyA.vehicleType.Car)
End Sub
Private Sub bicycleButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bicycleButton.Click
ButtonClickCode(TrafficSurveyA.vehicleType.Bicycle)
End Sub
Private Sub lorryButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lorryButton.Click
ButtonClickCode(TrafficSurveyA.vehicleType.Lorry)
End Sub
Private Sub ButtonClickCode(ByVal incrementVehicle As TrafficSurveyA.vehicleType)
fTrafficSurveyA.incrementCount(incrementVehicle)
Call updateView()
End Sub