简体   繁体   中英

C# - Convert Switch statement to If-Else

I'm having a bit of trouble getting going on this particular assignment. Taking a switch statement and converting it to if-else. This program utilizes a list box to select the location and show the corresponding time zone.

if (cityListBox.SelectedIndex != -1)
        {
            //Get the selected item.
            city = cityListBox.SelectedItem.ToString();

            // Determine the time zone.
            switch (city)
            {
                case "Honolulu":
                    timeZoneLabel.Text = "Hawaii-Aleutian";
                    break;
                case "San Francisco":
                    timeZoneLabel.Text = "Pacific";
                    break;
                case "Denver":
                    timeZoneLabel.Text = "Mountain";
                    break;
                case "Minneapolis":
                    timeZoneLabel.Text = "Central";
                    break;
                case "New York":
                    timeZoneLabel.Text = "Eastern";
                    break;
            }
        }
        else
        {
            // No city was selected.
            MessageBox.Show("Select a city.");

With this approach you can get rid of switch or if-else statements

Create a class to represent timezones

public class MyTimezone
{
    public string City { get; set; }
    public string Name { get; set; }
}

Create a list of timezones and bind it to the listbox

var timezones = new[]
{
    new MyTimezone { City = "Honolulu", Name = "Hawaii-Aleutian" },
    new MyTimezone { City = "San Francisco", Name = "Pacific" },
    // and so on...
}   

cityListBox.DisplayMember = "City";
cityListBox.ValueMember = "Name"; 
cityListBox.DataSource = timezones;

Then in the code where you want to use selected timezones

var selected = (MyTimeZone)cityListBox.SelectedItem;
timeZoneLabel.Text = selected.Name;

Because Name property used as ValueMember you can use SelectedValue property.

// SelectedValue can bu null if nothing selected
timeZoneLabel.Text = cityListBox.SelectedValue.ToString();

So, in most programming languages, a switch statement and an if-else statement are nearly one in the same (generally speaking; a switch may be quicker on certain compilers for certain languages, and I'm not sure about C# in particular). Switch is more or less syntactic sugar over an if-else . At any rate, an if-else statement corresponding to your switch would look something like this:

if (city == "Honolulu") {
    timeZoneLabel.Text = "Hawaii-Aleutian";
} else if (city == "San Francisco") {
    timeZoneLabel.Text = "Pacific";
} else if (city == "Denver") {
    timeZoneLabel.Text = "Mountain";
}
... etc

Does that make sense?

I suggest turning switch into a Dictionary<string, string> , ie separate data (city and its timezone) and representation ( Label , ListBox etc.):

private static Dictionary<string, string> s_TimeZones = new Dictionary<string, string>() {
  {"Honolulu", "Hawaii-Aleutian"},
  {"San Francisco", "Pacific"},
  //TODO: add all the pairs City - TimeZone here
};

then you can use it as follow (two if s):

if (cityListBox.SelectedIndex >= 0) {
  if (s_TimeZones.TryGetValue(cityListBox.SelectedItem.ToString(), out string tz))
    timeZoneLabel.Text = tz;
  else 
    timeZoneLabel.Text = "Unknown City";
} 
else {
  // No city was selected.
  MessageBox.Show("Select a city.");
  ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM