Il doit sûrement y avoir de nombreuses façons d'optimiser le code suivant, où je dois essentiellement à faire en sorte que beaucoup de zones de texte ne sont pas vides et lire leurs valeurs:Comment optimiser ce code?
if (foo1.Text.Length != 0 & bar1.Text.Length != 0)
{
output.Text += myStrings[i] + "/" + foo1.Text + "/" + bar1.Text;
}
if (foo2.Text.Length != 0 & bar2.Text.Length != 0)
{
output.Text += myStrings[i] + "/" + foo2.Text + "/" + bar2.Text;
}
if (foo3.Text.Length != 0 & bar3.Text.Length != 0)
{
output.Text += myStrings[i] + "/" + foo3.Text + "/" + bar3.Text;
}
if (foo4.Text.Length != 0 & bar4.Text.Length != 0)
{
output.Text += myStrings[i] + "/" + foo4.Text + "/" + bar4.Text;
}
if (foo5.Text.Length != 0 & bar5.Text.Length != 0)
{
output.Text += myStrings[i] + "/" + foo5.Text + "/" + bar5.Text;
}
if (foo6.Text.Length != 0 & bar6.Text.Length != 0)
output.Text += myStrings[i] + "/" + foo6.Text + "/" + bar6.Text;
if (foo7.Text.Length != 0 & bar7.Text.Length != 0)
{
output.Text += myStrings[i] + "/" + foo7.Text + "/" + bar7.Text;
}
if (foo8.Text.Length != 0 & bar8.Text.Length != 0)
{
output.Text += myStrings[i] + "/" + foo8.Text + "/" + bar8.Text;
}
if (foo9.Text.Length != 0 & bar9.Text.Length != 0)
{
output.Text += myStrings[i] + "/" + foo9.Text + "/" + bar9.Text;
}
if (foo10.Text.Length != 0 & bar10.Text.Length != 0)
{
output.Text += myStrings[i] + "/" + foo10.Text + "/" + bar10.Text;
}
Ceci est une très bonne solution! De plus, les éléments sont nommés en séquence, ce qui facilite le processus. Je vous remercie! – BeefTurkey
En règle générale, je n'aime pas cette approche en raison de l'association implicite et non évidente entre fos et barres. Je suggère de rendre l'association explicite via une classe FooBar et de créer une liste pour une utilisation ultérieure. –