none
convert IF to SWITCH RRS feed

  • Question

  • Hi,

    I have IF statement using C# like this:

    if (MSSQLreader["id_type"].ToString() == "CPR")
        MySQL_command.Parameters.Add("?id_type", MySqlDbType.VarChar, 50).Value = "BHID";
    else if (MSSQLreader["id_type"].ToString() == "PASSPORT")
        MySQL_command.Parameters.Add("?id_type", MySqlDbType.VarChar, 50).Value = "PSPT";
    else if (MSSQLreader["id_type"].ToString() == "OTHER")
        MySQL_command.Parameters.Add("?id_type", MySqlDbType.VarChar, 50).Value = "OTHR";
    else if (MSSQLreader["id_type"].ToString() == "DRIVING LICENSE")
        MySQL_command.Parameters.Add("?id_type", MySqlDbType.VarChar, 50).Value = "DRVL";
    else if (MSSQLreader["id_type"].ToString() == "HEALTH CARD")
        MySQL_command.Parameters.Add("?id_type", MySqlDbType.VarChar, 50).Value = "HLTH";
    else
        MySQL_command.Parameters.Add("?id_type", MySqlDbType.VarChar, 50).Value = "OTHR";
    

    I want to convert it to switch and it's clear how to do it except the last else?

    How can I say if not a, b, c, d else do it null (last else) using SWITCH?

    Saturday, September 15, 2012 9:59 AM

Answers

  • Hello, For last else you can use Default statement of Switch. 

    switch (value) 
    {
       //Case
    
       default:
                //Some logic
                break;
    }

    If any condition is not met, this can be handle in default

    HTH


    please Mark as the Answer, If this answers your question. If this post is helpful, please vote as helpful.

    • Marked as answer by Jassim Rahma Saturday, September 15, 2012 2:21 PM
    Saturday, September 15, 2012 10:23 AM
  • Here's how I'd put it together:

    string idType;
    
    // convert id_type
    switch (MSSQLreader["id_type"].ToString())
    {
        case "CPR": idType = "BHID"; break;
        case "PASSPORT": idType = "PSPT"; break;
        case "DRIVING LICENSE": idType = "DRVL"; break;
        case "HEALTH CARD": idType = "HLTH"; break;
     // case "OTHER": (handled same as below)          
        default: idType = "OTHR"; break;
    }
    
    MySQL_command.Parameters.Add("?id_type", MySqlDbType.VarChar, 50).Value = idType;

    Since "OTHER" and anything else both yield "OTHR" you don't need an explicit test.  However in these cases I always leave it in as a comment to show it's a 'known value'



    This signature unintentionally left blank.

    • Marked as answer by Jassim Rahma Sunday, September 16, 2012 6:06 PM
    Sunday, September 16, 2012 5:54 PM

All replies

  • Hello, For last else you can use Default statement of Switch. 

    switch (value) 
    {
       //Case
    
       default:
                //Some logic
                break;
    }

    If any condition is not met, this can be handle in default

    HTH


    please Mark as the Answer, If this answers your question. If this post is helpful, please vote as helpful.

    • Marked as answer by Jassim Rahma Saturday, September 15, 2012 2:21 PM
    Saturday, September 15, 2012 10:23 AM
  • Here's how I'd put it together:

    string idType;
    
    // convert id_type
    switch (MSSQLreader["id_type"].ToString())
    {
        case "CPR": idType = "BHID"; break;
        case "PASSPORT": idType = "PSPT"; break;
        case "DRIVING LICENSE": idType = "DRVL"; break;
        case "HEALTH CARD": idType = "HLTH"; break;
     // case "OTHER": (handled same as below)          
        default: idType = "OTHR"; break;
    }
    
    MySQL_command.Parameters.Add("?id_type", MySqlDbType.VarChar, 50).Value = idType;

    Since "OTHER" and anything else both yield "OTHR" you don't need an explicit test.  However in these cases I always leave it in as a comment to show it's a 'known value'



    This signature unintentionally left blank.

    • Marked as answer by Jassim Rahma Sunday, September 16, 2012 6:06 PM
    Sunday, September 16, 2012 5:54 PM
  • Avoid switch whereever and whenever possible!!!

    

    private string AvoidSwitchesAtAllCosts()
    {
        var temp = MSSQLreader["id_type"];
        return object.Equals(temp,"CPR") ? "BHID"
            : object.Equals(temp,"PASSPORT") ? "PSPT"
            : object.Equals(temp,"DRIVING LICENSE") ? "DRVL"
            : object.Equals(temp, "HEALTH CARD") ? "HLTH"
            : "OTHR";
    }

    or even better...

    readonly IDictionary<object, string> IdHash =
        new Dictionary<object, string>
        {
            {"CPR" , "BHID"},
            {"PASSPORT" , "PSPT"},
            {"DRIVING LICENSE" , "DRVL"},
            {"HEALTH CARD" , "HLTH"}
        };
    const string DEFAULT_ID_HASH = "OTHR";
    private string AnotherAvoidSwitchesAtAllCosts()
    {
        string result;
        IdHash.TryGetValue(MSSQLreader["id_type"], out result);
        return result ?? DEFAULT_ID_HASH;
    }


    gimme some slamming techno!!!!

    Sunday, September 16, 2012 6:49 PM
    Moderator