If语句,其中多个变量以数字结尾

  • 本文关键字:变量 数字 结尾 语句 If c#
  • 更新时间 :
  • 英文 :


变量:

private string filePath1 = null;
private string filePath2 = null;
private string filePath3 = null;
private string filePath4 = null;
private string filePath5 = null;
private string filePath6 = null;
private string filePath7 = null;
private string filePath8 = null;
private string filePath9 = null;
private string filePath10 = null;

当前If语句

if (string.IsNullOrEmpty(filePath1))
{
errors.Add("File Not Attached");
}
if (string.IsNullOrEmpty(filePath2))
{
errors.Add("File Not Attached");
}
....

问题:

而不是为每个变量设置多个if语句。如何创建一个if语句来遍历所有这些变量?

类似这样的东西:

if (string.IsNullOrEmpty(filePath + range(1 to 10))
{
errors.Add("File Not Attached");
}

您可以使用Reflection来实现这一点。对于这种情况,这显然是不鼓励的,因为其他答案提供了更好的解决方案,只是想向你展示它按照你想要的方式是可行的(这并不意味着这是正确的方式(

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

用法:

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);
}
}

在几乎所有使用后缀不同的变量的情况下,都应该使用集合(数组、列表…(。这就是其中之一。我将使用一个列表来回答这个问题,但任何集合都足够了。

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

然后,您可以使用循环来迭代您的列表:

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

创建一个新的arraylist,将所有文件路径添加到其中(或用所有文件路径初始化它(,并在数组中的元素上循环(用于每个循环(。对于每个元素,检查是否为nullOrEmpty,如果为yes,则添加到错误字符串中。

ArrayList arrlist = new ArrayList();
arrList.add(filePath1);
arrList.add(filePath2);
arrList.add(filePath3);
arrList.add(filePath4);
arrList.add(filePath5);
arrList.add(filePath6);
arrList.add(filePath7);
arrList.add(filePath8);
arrList.add(filePath9);
arrList.add(filePath10);
foreach (string element in arrList)
{
if (string.IsNullOrEmpty(element)
{
errors.Add("File Not Attached");
}
}

ps。您可能需要在每次错误后打印一行:

errors.Add("File Not Attachedn");
// Create list
List<string> filePaths = new List<string>;
//Add path in list like 
filePaths.add(filePath1);
//Check for null path here
foreach (string filepath in filePaths)
{
if (string.IsNullOrEmpty(filepath)
{
errors.Add("File Not Attached");
}
}

为了以相同的方式处理所有字符串,它们必须在某个集合中。

using System.Linq;
...
string[] allPaths = new string[10];
// Do something with these ten paths...
if (allPaths.Any(x => string.IsNullOrEmpty(x))
errors.Add("File Not Attached");

正如每隔一个答案所述,您应该使用集合。

如果你真的想坚持使用字段名称,你可以使用反射,但我强烈建议使用集合而不是反射

// using System.Reflection;
// Below code is meant to be used in a method of the class that holds the fields.
for (int i = 1; i <= 10; i++)
{
if (string.IsNullOrEmpty(this.GetType()
.GetField($"filePath{i}",
BindingFlags.NonPublic | BindingFlags.Instance)?
.GetValue(this))
{
errors.Add("File Not Attached");
}
}

如果你能创建这些变量类字段,我会投票给Innat3的Answer。

但是,如果这是不可能的,你不能让这些变量类字段,那么我建议你做如下操作:

class Program
{
static void Main(string[] args)
{
Dictionary<string, int> names = new Dictionary<string,int>();

for (int i = 0; i < 10; i++)
{
names.Add(String.Format("name{0}", i.ToString()), i);
}
var xx1 = names["name1"];
var xx2 = names["name2"];
var xx3 = names["name3"];
}
}

因为在c#中,我们不能动态计算变量名。

希望这能有所帮助。

最新更新