question

bootsy1974-0959 avatar image
0 Votes"
bootsy1974-0959 asked YijingSun-MSFT answered

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

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;
    
   }
dotnet-aspnet-general
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AlbertKallal-4360 avatar image
0 Votes"
AlbertKallal-4360 answered

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


image.png (14.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

YijingSun-MSFT avatar image
0 Votes"
YijingSun-MSFT answered

Hi @bootsy1974-0959 ,
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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.