Problem in changing the if block code to Switch case.

Apr 27 2011 6:15 AM
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)