1

我有这个代码:

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

新代码有更多的代码行。
新版本是更好的做法,还是我应该遵循第三种模式?


编辑

使用 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
4

1 回答 1

2

由于事件处理程序中的代码是相同的,因此您可以只为所有事件使用一个事件处理程序:

Private Sub vehicleButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles carButton.Click, bicycleButton.Click, lorryButton.Click
  Dim mySender As Button = CType(sender, 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

另一种选择是保留单独的事件处理程序,并利用每个事件处理程序可以将正确的文本发送到公共方法的事实,因此您无需检查控件名称来确定文本:

Private Sub carButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles carButton.Click
  ButtonClickCode("Car")
End Sub

Private Sub bicycleButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bicycleButton.Click
  ButtonClickCode("Bicycle")
End Sub

Private Sub lorryButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lorryButton.Click
  ButtonClickCode("Lorry")
End Sub

Private Sub ButtonClickCode(ByVal incrementVehicle As String)
  fTrafficSurveyA.incrementCount(incrementVehicle)
  Call updateView()
End Sub
于 2012-12-21T08:53:46.813 回答