If statement with multiple variables ending with a number

You can achieve this using Reflection. This is obviously discouraged for this scenario, as the other answers provide better solutions, just wanted to show you it's doable the way you intended it to be done (which doesn't mean it's the correct way)

public class Test
{
    private string filePath1 = null;
    private string filePath2 = null;
    private string filePath3 = null;
}

Usage:

Test obj = new Test();

//loop through the private fields of our class
foreach (var fld in obj.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
                                     .Where(x => x.Name.StartsWith("filePath"))) // filter
{
    if (string.IsNullOrEmpty(fld.GetValue(obj) as string))
    {
        errors.Add("File Not Attached in variable: " + fld.Name);
    }
}

In nearly all cases where you're using variables with a differently numbered suffix, you should really be using a collection (array, list, ...). This is one of those cases. I'll be using a list for this answer but any collection will suffice.

private List<string> filePaths = new List<string>()
                                 {
                                     "path1",
                                     "path2",
                                     "path3",
                                     "path4"
                                 };

You can then use a loop to iterate over your list:

foreach (string path in filePaths)
{
    if(String.IsNullOrEmpty(path))
        errors.Add("File not attached");
}

Tags:

C#