英文:
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。
您是否知道更优化的方法,例如循环检查包含所有成员昵称的列表?希望您明白我的意思
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
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<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 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论