如何过滤掉国家代码?

发布于 2024-09-06 08:05:52 字数 531 浏览 1 评论 0原文

好的,这就是我正在尝试的。

我有一个包含世界上所有国家/地区的下拉列表。 用户选择一个,然后将该值传递给 case 语句, 如果大小写相符,则会发送一封电子邮件。

我只有四种不同的配方,但我可以有多达几十种 与四封电子邮件中的每一封相匹配的国家/地区。例如:

switch (selectedCountry)
{
    case "ca":
        sendTo = canadaEmail;
        break;
    case "uk":
    case "de":
    case "fr"
        sendTo = europeanEmail;
        break;
    case "bb":                
        sendTo = barbadosEmail;
        break;
    default:
        sendTo = usEmail;
        break;
}

我想知道的是,有什么比这样做更好的方法? 一份巨大的案件陈述?

Okay, here's what I'm attempting.

I have a drop down list with all of the countries in the world.
The user selects one, and that value is passed to a case statement,
if it matches the case, an email is sent.

I only have four different recipents, but I can have upwards to dozens
of countries to match with each one of the four emails. For example:

switch (selectedCountry)
{
    case "ca":
        sendTo = canadaEmail;
        break;
    case "uk":
    case "de":
    case "fr"
        sendTo = europeanEmail;
        break;
    case "bb":                
        sendTo = barbadosEmail;
        break;
    default:
        sendTo = usEmail;
        break;
}

What I would like to know is, what's a better way of doing this rather than having
one huge case statement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧竹 2024-09-13 08:05:52

您可以使用字典:

Dictionary<string, string> sendToEmails = new Dictionary<string, string>();
sendToEmails["bb"] = barbadosEmail;
sendToEmails["ca"] = canadaEmail;
sendToEmails["uk"] = europeanEmail;
sendToEmails["de"] = europeanEmail;

然后使用 TryGetValue 获取值当您需要时:

string sendTo;
if (sendToEmails.TryGetValue(selectedCountry, out sendTo))
{
    // Send the email here.
}

此方法的一个优点是您的字典不必硬编码到您的程序中。它可以很容易地来自配置文件或数据库。

如果您选择数据库路线,您还可以考虑使用 LINQ:

string sendTo = dc.CountryEmails
                  .SingleOrDefault(c => c.Country == selectedCountry);

You can use a dictionary instead:

Dictionary<string, string> sendToEmails = new Dictionary<string, string>();
sendToEmails["bb"] = barbadosEmail;
sendToEmails["ca"] = canadaEmail;
sendToEmails["uk"] = europeanEmail;
sendToEmails["de"] = europeanEmail;

Then use TryGetValue to get the value when you need it:

string sendTo;
if (sendToEmails.TryGetValue(selectedCountry, out sendTo))
{
    // Send the email here.
}

One advantage of this method is that your dictionary doesn't have to be hard coded into your program. It could just as easily come from a configuration file or a database.

If you choose the database route you could also consider using LINQ:

string sendTo = dc.CountryEmails
                  .SingleOrDefault(c => c.Country == selectedCountry);
空气里的味道 2024-09-13 08:05:52

您无法回避这样一个事实:在某个地方、以某种方式,您必须枚举国家/地区并将它们分配给电子邮件地址。您可以通过多种方式来实现这一点,无论是数据库、外部 XML 文件还是内部 List 对象。

例如:

List<string> europeanEmailCountries = new List<string>();
europeanEmailCountries.AddRange("fr", "de"); // etc

...

if(europeanEmailCountries.Contains(countryCode))
{
    sendTo = europeanEmailAddress;
}

这允许您定义映射到特定电子邮件地址的国家/地区列表,而无需遍历每个潜在条目,从而节省了复杂的 switch 语句。不过,我可能倾向于从 XML 文件填充列表,而不是对值进行硬编码。

You can't get around the fact that somewhere, somehow, you'll have to enumerate the countries and assign them to an email address. You could do that in any number of ways, be it a database, an external XML file, or an internal List object.

For example:

List<string> europeanEmailCountries = new List<string>();
europeanEmailCountries.AddRange("fr", "de"); // etc

...

if(europeanEmailCountries.Contains(countryCode))
{
    sendTo = europeanEmailAddress;
}

This saves you the convoluted switch statement by allowing you to define a list of countries mapped to a particular email address, without going through each potential entry. I might be inclined to populate the list from an XML file instead of hardcoding the values, though.

所有深爱都是秘密 2024-09-13 08:05:52

有几个选项可以消除这种情况:

  1. 将映射存储在数据库中并按国家/地区代码查找它们。
  2. 在您的代码中构建一个地图,其中国家/地区代码作为键,电子邮件作为值。

A couple options that would eliminate the cases:

  1. Store the mappings in a database and look them up by country code.
  2. Build a map in your code with the country code as the key and the email as the value.
岁吢 2024-09-13 08:05:52

将其外部化(XML、数据库,无论您喜欢什么......)并仅实现一个选择正确状态机的“状态机”。

Externalize it (XML, database, whatever you like...) and only implement a "state machine" which chooses the right one.

原谅我要高飞 2024-09-13 08:05:52

是的——如果你在某个地方有一个硬编码列表,请考虑使用自定义类型的集合:

public class MyType
{
    public string Code { get; set; }
    public string Email { get; set; }
}

那么你可以这样做:

List<MyType> myList = new List<MyType>()
{
    new MyType() { Code = "A", Email = "something" },
    // etc..
}

string emailAddress = myList.Where(m => m.Code == selectedCountry);

虽然,我想说这是非常糟糕的设计,我会鼓励你使用 RDBMS某种。

Yes -- if you have a hard coded list somewhere, consider using a collection of a custom type:

public class MyType
{
    public string Code { get; set; }
    public string Email { get; set; }
}

Then you could do something like this:

List<MyType> myList = new List<MyType>()
{
    new MyType() { Code = "A", Email = "something" },
    // etc..
}

string emailAddress = myList.Where(m => m.Code == selectedCountry);

Although, I'd say this is very poor design and I would encourage you to use a rdbms of some sort.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文