如何优化这些条件 IF

huangapple go评论135阅读模式
英文:

How to optimize these conditions IF

问题

I have a DataGridView that receives and displays IDs from an external source. I know the nicknames of each ID.

在“ID”列中,您如何优化以查看哪个昵称对应于单元格ID 0?

这是我所做的示例(见下文),它运行良好,但IF条件太长,因为有近200个带有昵称的ID。

您是否知道更优化的方法,例如循环检查包含所有成员昵称的列表?希望您明白我的意思 如何优化这些条件 IF

string id = row.Cells[0].Value.ToString();
var nom = "";

if (id == "FB23E1A8B7E2944FAAEC6219BBDF8243")
{
   nom = "Pseudo 1";
}
else if (id == "FE63D6040E22611D978B73064B3A2057")
{
   nom = "Pseudo 2";
}
else if (id == "0D44A8E3F29D9E568FE31C7DE45A80E0")
{
   nom = "Pseudo 3";
}

如果您需要更多帮助,请告诉我。

英文:

I have a DataGridView that receives and displays IDs from an external source. I know the nicknames of each ID.

In the "ID" column, how would you optimize to see which nickname corresponds to the cell ID 0

Here is an example of what I did (see below) which works well but the IF conditions are too long because there are nearly 200 IDs with their nicknames.

Do you know a more optimized way like a loop that checks a list that contains all the nicknames of the members ? I hope you understood what I mean 如何优化这些条件 IF

 string id = row.Cells[0].Value.ToString();
 var nom = "";

 if (id == "FB23E1A8B7E2944FAAEC6219BBDF8243")
 {
    nom = "Pseudo 1";
 }
 else if (id == "FE63D6040E22611D978B73064B3A2057")
 {
   nom = "Pseudo 2";
 }
 else if (id == "0D44A8E3F29D9E568FE31C7DE45A80E0")
 {
   nom = "Pseudo 3";
 }

Thanks

答案1

得分: 2

Use a Dictionary<string, string>:

var idNickNameMapping = new Dictionary<string, string>
{
    {"FB23E1A8B7E2944FAAEC6219BBDF8243", "Pseudo 1"}, {"FE63D6040E22611D978B73064B3A2057", "Pseudo 2"}, {"0D44A8E3F29D9E568FE31C7DE45A80E0", "Pseudo 3"}
};

string nom = idNickNameMapping.TryGetValue(id, out string name) ? name : null;

If the dictionary never changes, make it a field in the class, for example:

private static readonly Dictionary<string, string> IdNickNameMapping = new Dictionary<string, string>
{
    {"FB23E1A8B7E2944FAAEC6219BBDF8243", "Pseudo 1"}, {"FE63D6040E22611D978B73064B3A2057", "Pseudo 2"}, {"0D44A8E3F29D9E568FE31C7DE45A80E0", "Pseudo 3"}
};

If you have 200 entries, like you've said, it might be better to store them in a file or database as Rand said in a comment. You can load them into the dictionary on application start. The collection initializer I have shown is just one way to fill a dictionary; you can also use a simple loop where you use the Add method. If you use a static field, the best place is the static constructor of the class.

英文:

Use a Dictionary&lt;string, string&gt;:

var idNickNameMapping = new Dictionary&lt;string, string&gt;
{
	{&quot;FB23E1A8B7E2944FAAEC6219BBDF8243&quot;, &quot;Pseudo 1&quot;}, {&quot;FE63D6040E22611D978B73064B3A2057&quot;, &quot;Pseudo 2&quot;}, {&quot;0D44A8E3F29D9E568FE31C7DE45A80E0&quot;, &quot;Pseudo 3&quot;}
};

string nom = idNickNameMapping.TryGetValue(id, out string name) ? name : null;

If the dictionary never changes make it a field in the class, for example:

private static readonly Dictionary&lt;string, string&gt; IdNickNameMapping = new Dictionary&lt;string, string&gt;
{
	{&quot;FB23E1A8B7E2944FAAEC6219BBDF8243&quot;, &quot;Pseudo 1&quot;}, {&quot;FE63D6040E22611D978B73064B3A2057&quot;, &quot;Pseudo 2&quot;}, {&quot;0D44A8E3F29D9E568FE31C7DE45A80E0&quot;, &quot;Pseudo 3&quot;}
};

If you have 200 entries, like you've said, it might be better to store them in a file or database as Rand said in a comment. You can load them into the dictionary on application start. The collection initializer i have shown is just one way to fill a dictonary, you can also use a simple loop where you use the Add method. If you use a static field the best place is the static constructor of the class.

huangapple
  • 本文由 发表于 2023年5月17日 20:43:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272250.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定