Condensazione del codice vb.net.
-
28-09-2020 - |
Domanda
Ho il seguente codice:
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
.
Sto cercando di consolidare questo in un sottogruppo senza avere alcun codice di ripetizione (tutti i sottomissione attualmente tengono generatori btnSubmitUIDisabled()
e lblStatusClear()
)
Ho pensato a una dichiarazione CASE
ma avrebbe anche un codice ripetitivo.Questa è un'applicazione WPF e tutti gli eventi "TextChanged" si trovano nella XAML, quindi senza "maniglie" alla fine di ogni sub.
Grazie in anticipo.
Soluzione
Bene per una cosa che puoi cascata le chiamate:
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
.
li nominai personalmente in modo diverso a quel punto - fai dire il metodo cosa fa piuttosto che ciò che reagisce a - ma è solo una lunga differenza diopinione tra me stesso e studio visivo.
Altri suggerimenti
Pseudo Codice come non così tanto di un 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
.
Non carino ma un metodo.
Modifica: una cosa che non ho detto è che vorresti lanciare il mittente a un tipo di testo in casella di testo in modo da poter ottenere la proprietà del nome.
Se vuoi che tutto sia in un metodo, allora @Spinon risponde è un modo.Un altro è scrivere un metodo di estensione per ridurre la quantità di dichiarazioni di orelse necessità di scrivere e rende il codice un po 'più leggibile.
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
.