3
Answers

Problem in changing the if block code to Switch case.

K S Reddi Prasad

K S Reddi Prasad

13y
1.7k
1
Hi all,

         I have some code in  multiple 'if' blocks, the same functionality i want to do it in Switch case. I tried but i  am getting some Exception Here.
                 
        I am Having if code like the Following:

        Office.DocumentProperties docProp = (Office.DocumentProperties)
                                                 Globals.ThisAddIn.Application.ActiveDocument.CustomDocumentProperties;
      

                if (docProp["ReferenceId"] != null)
                {
                    labelControl = (RibbonLabel)Globals.Ribbons[0].Tabs[0].Groups[0].Items[3];
                    labelControl.Label = docProp["ReferenceId"].Value.ToString();
                }

                if (docProp["ReferenceType"] != null)
                {
                    labelControl = (RibbonLabel)Globals.Ribbons[0].Tabs[0].Groups[0].Items[4];
                    labelControl.Label = docProp["ReferenceType"].Value.ToString();
                    labelReferenceId = (RibbonLabel)Globals.Ribbons[0].Tabs[0].Groups[0].Items[0];
                }

                if (docProp["CreatedBy"] != null)
                {
                    labelControl = (RibbonLabel)Globals.Ribbons[0].Tabs[0].Groups[0].Items[5];
                    labelControl.Label = docProp["CreatedBy"].Value.ToString();
                }

               

        for the above code,  I did like this:

                string[] customProperties = { "ReferenceId", "ReferenceType", "CreatedBy"};

                foreach(string customProperty in customProperties)
                {
                    switch (docProp["customProperty"])
                   {
                       case "ReferenceId":   labelControl = (RibbonLabel)Globals.Ribbons[0].Tabs[0].Groups[0].Items[3];
                                                    labelControl.Label = docProp["ReferenceId"].Value.ToString();
                                                    break;

                       case "ReferenceType":       labelControl = (RibbonLabel)Globals.Ribbons[0].Tabs[0].Groups[0].Items[4];
                                                            labelControl.Label = docProp["ReferenceType"].Value.ToString();
                                                            labelReferenceId = (RibbonLabel)Globals.Ribbons[0].Tabs[0].Groups[0].Items[0];
                                                            break;
                        case "CreatedBy":    labelControl = (RibbonLabel)Globals.Ribbons[0].Tabs[0].Groups[0].Items[4];
                                                    labelControl.Label = docProp["ReferenceType"].Value.ToString();
                                                    labelReferenceId = (RibbonLabel)Globals.Ribbons[0].Tabs[0].Groups[0].Items[0];
                                                    break;
                       default:
                                           break;

                  }
                }

  Any help will be greatly appreciated.

Thanks,
K.S.Reddi Prasad.


Answers (3)