How do I convert switch case into a lookup table with keys, values, etc.

bootsy1974 1 Reputation point
2021-10-15T21:00:21.807+00:00

I'm not sure how to convert this switch case to a lookup table. How do I do that?

getContactTypeTitle(contactTypeCode: string): string {

    let contactType = 'Site Contact';

    switch (contactTypeCode) {

      case 'SiteApp_CACFP_PrincipleDirector':

        contactType = 'Principle Director';

        break;

      case 'SiteApp_CACFP_PrincipleAsstDir':

        contactType = 'Principle Assistant Director';

        break;

      case 'SiteApp_CACFP_Cook':

        contactType = 'Cook';

        break;

    }

    return contactType;

  }

 
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,254 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Albert Kallal 4,651 Reputation points
    2021-10-15T22:58:09.873+00:00

    We assume a database here?

    Ok, then first we assume the lookup table - say like this:

    140956-image.png

    so, nice about above is we can add lots more, not have to change code.

    c# code for above then becomes:

        string GetContactType(string sName)  
        {  
            string Result = "Site Contact";  
            if (sName == "")  
                return Result;  
      
            using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))  
            {  
                string strSQL   
                    = "SELECT Description from ContactTypes where ContactType = @ConType";  
                using(SqlCommand cmdSQL = new SqlCommand(strSQL,conn))  
                {  
                    cmdSQL.Parameters.Add("@ConType", SqlDbType.NVarChar).Value = sName;  
                    conn.Open();  
                    object s = cmdSQL.ExecuteScalar();  
                    if (s != null)  
                        Result = s.ToString();  
                }  
            }  
            return Result;  
        }  
    

    So now you can go say:

    TextBox2.Text = GetContactType("SiteApp_CACFP_Cook");

    And the result should be cook.

    Regards,
    Albert D. Kallal (Access MVP 2003-2017)
    Edmonton, Alberta Canada

    0 comments No comments

  2. Yijing Sun-MSFT 7,066 Reputation points
    2021-10-18T02:58:12.347+00:00

    Hi @bootsy1974 ,
    A Map lookup would be pretty simple to implement.

    const contactTypeByCode = new Map([  
        ['SiteApp_CACFP_PrincipleDirector', 'Principle Director'],  
        ['SiteApp_CACFP_PrincipleAsstDir', 'Principle Assistant Director'],  
        ['SiteApp_CACFP_Cook', 'Cook'],  
    ]);  
    const getContactTypeTitle = (contactTypeCode: string) {  
        return contactTypeByCode.get(contactTypeCode) || 'Site Contact';  
    };  
    

    You might wish to increase the type-safety by requiring the caller to pass a code that's in the Map already.

    const contactTypeByCodeArr = [  
        ['SiteApp_CACFP_PrincipleDirector', 'Principle Director'],  
        ['SiteApp_CACFP_PrincipleAsstDir', 'Principle Assistant Director'],  
        ['SiteApp_CACFP_Cook', 'Cook'],  
    ] as const;  
    type ContactCode = typeof contactTypeByCodeArr[number][0];  
    const contactTypeByCode = new Map(contactTypeByCodeArr);  
    const getContactTypeTitle = (contactTypeCode: ContactCode) {  
        return contactTypeByCode.get(contactTypeCode);  
    };  
    

    Best regards,
    Yijing Sun


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments