What's a more efficient way to error check my UI without copy/pasting the same code over and over again?
I'm currently using repetitive else if () statements and I'm sure they're a more efficient way of doing it. I thought about using for loops but the different UI names makes it hard to implement into a for loop.
private void BoatSubmitButton_Click(object sender, EventArgs e)
{
if (BoatNameTextBox.Text == "")
{
MessageBox.Show("Please Input Name", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (Catch1ComboBox.SelectedIndex != -1 && NumericAmount1.Value == 0)
{
MessageBox.Show("Please Input Amount Of Fish Caught", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (Catch2ComboBox.SelectedIndex != -1 && NumericAmount2.Value == 0
)
{
MessageBox.Show("Please Input Amount Of Fish Caught", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (Catch3ComboBox.SelectedIndex != -1 && NumericAmount3.Value == 0)
{
MessageBox.Show("Please Input Amount Of Fish Caught", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (Catch4ComboBox.SelectedIndex != -1 && NumericAmount4.Value == 0)
{
MessageBox.Show("Please Input Amount Of Fish Caught", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
An even more efficient way of error checking is using OR in the condition for example:
else if (Catch1ComboBox.SelectedIndex != -1 && NumericAmount1.Value == 0 ||
Catch2ComboBox.SelectedIndex != -1 && NumericAmount2.Value == 0 ||
Catch3ComboBox.SelectedIndex != -1 && NumericAmount3.Value == 0 ||
Catch4ComboBox.SelectedIndex != -1 && NumericAmount4.Value == 0)
{
MessageBox.Show("Please Input Amount Of Fish Caught", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}