I’ve got six text boxes, and as long as one has valid input in it I want my program to go ahead with import code. Problem is I don’t know what the most efficient way to do it is. The two alternatives I’ve come up with thus far are as follows:
Version 1:
//if no paths are specified the user is shown an error and the program
//will do nothing
if ((txtForecastFTE.Text.Length != 0)
|| (txtActualFTE.Text.Length != 0)
|| (txtForecastAHT.Text.Length != 0)
|| (txtActualAHT.Text.Length != 0)
|| (txtForecastVolume.Text.Length != 0)
|| (txtActualVolume.Text.Length != 0))
{
if (txtForecastFTE.Text.Length != 0)
{
//import code
}//end if
if (txtActualFTE.Text.Length != 0)
{
//import code
}//end if
if (txtForecastAHT.Text.Length != 0)
{
//import code
}//end if
if (txtActualAHT.Text.Length != 0)
{
//import code
}//end if
if (txtForecastVolume.Text.Length != 0)
{
//import code
}//end if
if (txtActualVolume.Text.Length != 0)
{
//import code
}//end if
}//end if
else
{
MessageBox.Show("You must enter the path for at least one file.");
}//end if-else
}//end import code
Version 2:
//if no paths are specified the user is shown an error and the program
//will do nothing
if (txtForecastFTE.Text.Length != 0)
{
pathTrue = true;
//import code
}//end if
if (txtActualFTE.Text.Length != 0)
{
pathTrue = true;
//import code
}//end if
if (txtForecastAHT.Text.Length != 0)
{
pathTrue = true;
//import code
}//end if
if (txtActualAHT.Text.Length != 0)
{
pathTrue = true;
//import code
}//end if
if (txtForecastVolume.Text.Length != 0)
{
pathTrue = true;
//import code
}//end if
if (txtActualVolume.Text.Length != 0)
{
pathTrue = true;
//import code
}//end if
if (!pathTrue)
{
MessageBox.Show("You must enter the path for at least one file.");
}//end if
}//end import code
Obviously I’ll add further validation (try-catch) to each file import section, but this is the basic gist.
Any help appreciated 🙂 If there are other options which would be more efficient than either of the two versions supplied please don’t hesitate to put it forward. Thanks guys.
I’d argue “efficiency” in this case shouldn’t be your goal, but rather simple, readable, non-redundant code.
Another approach, which might be more fitting upon inspection of your samples, could be