views:

43

answers:

3

I have the following code:

  Private Sub txtFileFromLocation_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs)
    MachineNameUIDisabled()
    ServiceNameUIDisabled()
    ToLocationUIDisabled()
    btnSubmitUIDisabled()
    lblStatusClear()
  End Sub

  Private Sub txtMachineName_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs)
    ServiceNameUIDisabled()
    ToLocationUIDisabled()
    btnSubmitUIDisabled()
    lblStatusClear()
  End Sub

  Private Sub txtServiceName_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs)
    ToLocationUIDisabled()
    btnSubmitUIDisabled()
    lblStatusClear()
  End Sub


  Private Sub txtFilesToLocation_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs)
    btnSubmitUIDisabled()
    lblStatusClear()
  End Sub

I am looking to consolidate this into one sub without having any repeating code (all subs currently hold btnSubmitUIDisabled() and lblStatusClear())

I thought about a CASE statement but that would also have repetitive code. This is a WPF application and all the "TextChanged" events are located in the xaml, thus no "Handles" at the end of each sub.

Thanks in advance.

+4  A: 

Well for one thing you can cascade the calls:

Private Sub txtFileFromLocation_TextChanged(ByVal sender As Object, _
                                            ByVal e As TextChangedEventArgs)
  MachineNameUIDisabled()
  txtMachineName_TextChanged()
End Sub

Private Sub txtMachineName_TextChanged(ByVal sender As Object, _
                                       ByVal e As TextChangedEventArgs)
  ServiceNameUIDisabled()
  txtServiceName_TextChanged()
End Sub

Private Sub txtServiceName_TextChanged(ByVal sender As Object, _
                                       ByVal e As TextChangedEventArgs)
  ToLocationUIDisabled()
  txtFilesToLocation_TextChanged()
End Sub


Private Sub txtFilesToLocation_TextChanged(ByVal sender As Object,
                                           ByVal e As TextChangedEventArgs)
  btnSubmitUIDisabled()
  lblStatusClear()
End Sub

I would personally name them differently at that point - make the method say what it does rather than what it reacts to - but that's just a long-running difference of opinion between myself and Visual Studio.

Jon Skeet
I was just about to click "Post Your Answer" with the exact same thing when the orange bar appeared on top...
Vedran
A: 

Pseudo code as not that much of a vb guy:

Private Sub txt_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs)

    if (sender.Name = txtServiceName)
    MachineNameUIDisabled()

    if (sender.Name = txtServiceName or sender.Name = txtMachineName)
    ServiceNameUIDisabled()

    if (sender.Name = txtServiceName or sender.Name = txtMachineName or sender.Name = txtFileFromLocation)
    ToLocationUIDisabled()

    btnSubmitUIDisabled()
    lblStatusClear()
  End Sub

Not pretty but one method.

EDIT: One thing I didn't mention is you would want to cast the sender to a textbox type so that you can get the name property.

spinon
A: 

If you want it all in one method then @spinon answer is one way. Another is write an extension method to reduce the amount of OrElse statements you need to write and it makes the code a bit more readable.

Private Sub txt_TextChanged(ByVal sender As Object, 
                            ByVal e As TextChangedEventArgs
                           )

    Dim textBox = TryCast(sender, TextBox)

    If textBox IsNot Nothing Then

        If textBox.IsOneOf(txtServiceName) Then
            MachineNameUIDisabled()
        End If

        If textBox.IsOneOf(txtServiceName, txtMachineName) Then
            ServiceNameUIDisabled()
        End If

        If textBox.IsOneOf(txtServiceName, txtMachineName, txtFileFromLocation) Then
            ToLocationUIDisabled()
        End If

        If textBox.IsOneOf(txtServiceName, txtMachineName, txtFileFromLocation, 
            txtFilesToLocation) Then

            btnSubmitUIDisabled()
            lblStatusClear()

        End If

    End If

End Sub

<Extension()>
Public Function IsOneOf(ByVal value As Object, 
                        ByVal ParamArray values() As Object
                       ) As Boolean

    If value Is Nothing Then
        Throw New ArgumentNullException("value")
    End If

    If values Is Nothing Then
        Throw New ArgumentNullException("values")
    End If

    Return values.Contains(value)

End Function
Tim Murphy