如何对超过1000个if语句执行检查? [英] How do I perform a check with over 1000 if statements?

查看:120
本文介绍了如何对超过1000个if语句执行检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一份报纸和杂志清单.精确到1374,我的所有文件都命名为abcd_20190406.xml,其中abcd是文件名abbreviaton,而20190406是yyyyMMdd格式的日期.

I have a list of newspapers and magazines. It counts to 1374 to be exact and all my files are named like abcd_20190406.xml, where abcd is the filename abbreviaton and 20190406 is the date in yyyyMMdd format.

所以我想要的是,如果我按WindowsForm中的按钮,它将在列表中搜索<​​c1>.如果找到,它将填充其各自的数据,这些数据将在我的表单的TextBoxes中提及.

So what I want is that, if I press the button in my WindowsForm, it will search for the abcd in the list. If found, it will fill it's respective data which will be mentioned in my form's TextBoxes.

if (Path.GetFileNameWithoutExtension(cboSource.Text).StartsWith("aamfr"))
{
        TextBoxPublication.Text = "Anti-âge Magazine";
        TextBoxAbbreviation.Text = "aamfr";
        TextBoxLanguage.Text="fr";
}

if (Path.GetFileNameWithoutExtension(cboSource.Text).StartsWith("wic"))
{
        TextBoxPublication.Text = "Wisden Cricket Monthly";
        TextBoxAbbreviation.Text = "wic";
        TextBoxLanguage.Text="en";
}

以此类推.这是我目前的做法.如果有条件,给予1374.那么,有没有更快或更更好的方法呢?

And so on. This is my current approach. Giving 1374 if conditions. So is there a faster way or a more better way of doing this?

我想创建一个List<string>并执行该操作,但是我不知道如何进行.我什至不知道这是否是正确的方法.

I thought of creating a List<string> and doing it but I have no clue how to proceed. I don't even know if that is the correct way of doing this.

请帮助.

推荐答案

创建一个类来保存每个出版物的信息,例如:

Create a class to hold the information for each publication, for example:

class PublicationInfo
{
    public string Title{ get; set; }
    public string Abbreviation{ get; set; }
    public string Language{ get; set; }
}

然后使用字典来保存所有出版物

Then use a dictionary to hold all your publications

var Publications = new Dictionary<string,PublicationInfo>();

,并使用缩写作为关键字,用您的数据填充它. 然后,您将能够像这样填充文本框:

and fill it with your data, using the abbreviation as the key. You will then be able to fill your text boxes like this:

var abbreviation = Path.GetFileNameWithoutExtension(cboSource.Text).Split("_")[0];
if (Publications.ContainsKey(abbreviation)
{
    TextBoxPublication.Text = Publications[abbreviation].Title;
    TextBoxAbbreviation.Text = abbreviation;
    TextBoxLanguage.Text = Publications[abbreviation].Language;
}

这篇关于如何对超过1000个if语句执行检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆