Chris
Chris

Reputation: 6325

How to filter off of a country code?

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?

Upvotes: 3

Views: 333

Answers (5)

Seb Charrot
Seb Charrot

Reputation: 2896

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.

Upvotes: 2

Mark Byers
Mark Byers

Reputation: 838156

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

Upvotes: 4

Ian P
Ian P

Reputation: 12993

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.

Upvotes: 0

Eric Petroelje
Eric Petroelje

Reputation: 60498

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.

Upvotes: 1

Lucero
Lucero

Reputation: 60190

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

Upvotes: 0

Related Questions