4
\$\begingroup\$

This is the current code that gets put into a option box, is there a way to make this code more efficient and use less code

<select name="searchTypeSelect" id="searchTypeSelect" size='1' class='searchUtilityMenu' >
        <option value='<%# ((string)HttpContext.Current.Session["whichMenu"] == "programmingPLUSearch") ? (string)HttpContext.Current.Session["pluNumberSearchCaptionValue"] : 
                  (
                   ((string)HttpContext.Current.Session["whichMenu"] == "programmingLabels") ? (string)HttpContext.Current.Session["labelsNumberSearchCaptionValue"] :
                   (
                    ((string)HttpContext.Current.Session["whichMenu"] == "programmingTextsSearch") ? (string)HttpContext.Current.Session["textsSearchNumberSearchCaptionValue"] : 
                    (
                     ((string)HttpContext.Current.Session["whichMenu"] == "programmingTraceability") ?  WorldViewNet.programming.TextsSearch.textsSearchNumberSearchCaptionValue : 
                     (
                      ((string)HttpContext.Current.Session["whichMenu"] == "programmingNotBarcodes") ? (string)HttpContext.Current.Session["barcodesNumberSearchCaptionValue"] : 
                      (
                       ((string)HttpContext.Current.Session["whichMenu"] == "programmingShops") ? WorldViewNet.programming.Shops.shopsNumberSearchCaptionValue : 
                       (
                        ((string)HttpContext.Current.Session["whichMenu"] == "programmingCustomers") ? WorldViewNet.programming.Customers.customersNumberSearchCaptionValue : 
                        (
                         ((string)HttpContext.Current.Session["whichMenu"] == "programmingNutrifacts") ? WorldViewNet.programming.Nutrifacts.nutrifactsNumberSearchCaptionValue : 
                         (
                          ((string)HttpContext.Current.Session["whichMenu"] == "programmingIngredients") ? WorldViewNet.programming.Ingredients.ingredientsNumberSearchCaptionValue : 
                          (
                           ((string)HttpContext.Current.Session["whichMenu"] == "programmingServings") ? WorldViewNet.programming.Servings.servingsNumberSearchCaptionValue : 
                           (
                            ((string)HttpContext.Current.Session["whichMenu"] == "programmingImagesSearch") ? (string)HttpContext.Current.Session["imagesSearchNumberSearchCaptionValue"] : 
                            (
                             ((string)HttpContext.Current.Session["whichMenu"] == "managementCurrency") ? (string)HttpContext.Current.Session["currencyNumberSearchCaptionValue"] : 
                             (
                              ((string)HttpContext.Current.Session["whichMenu"] == "serviceRejector") ? WorldViewNet.service.Rejector.rejectorsNumberSearchCaptionValue : 
                              (
                               ((string)HttpContext.Current.Session["whichMenu"] == "systemTemplates") ? (string)HttpContext.Current.Session["templatesNumberSearchCaptionValue"] : 
                               (
                                ((string)HttpContext.Current.Session["whichMenu"] == "managementTotals") ? WorldViewNet.management.Totals.totalsNumberSearchCaptionValue : 
                                (
                                 ((string)HttpContext.Current.Session["whichMenu"] == "managementCookingTimes") ? WorldViewNet.management.CookingTimes.cookingTimesNumberSearchCaptionValue : 
                                 (
                                  ((string)HttpContext.Current.Session["whichMenu"] == "managementTrays") ? WorldViewNet.management.Trays.traysNumberSearchCaptionValue : 
                                  (
                                   ((string)HttpContext.Current.Session["whichMenu"] == "managementAssignMachinePLU") ? WorldViewNet.management.AssignMachinePLU.assignPLUIDSearchCaptionValue : 
                                   (
                                    ((string)HttpContext.Current.Session["whichMenu"] == "serviceSettingsGeneral") ? WorldViewNet.service.settings.General.generalNumberSearchCaptionValue : 
                                    (
                                     ((string)HttpContext.Current.Session["whichMenu"] == "serviceSettingsDateTime") ? WorldViewNet.service.settings.DateTime.dateTimeNumberSearchCaptionValue :
                                     (
                                      ((string)HttpContext.Current.Session["whichMenu"] == "serviceDevicesLabellers") ? WorldViewNet.service.devices.Labellers.labellersNumberSearchCaptionValue : 
                                      (
                                       ((string)HttpContext.Current.Session["whichMenu"] == "serviceNetworkConnections") ? WorldViewNet.service.NetworkConnections.networkConnectionsNumberSearchCaptionValue : 
                                       (
                                        ((string)HttpContext.Current.Session["whichMenu"] == "serviceLabellers1") ? WorldViewNet.service.Labellers.labellersNumberSearchCaptionValue :
                                        (
                                         ((string)HttpContext.Current.Session["whichMenu"] == "serviceLabellers") ? WorldViewNet.service.Labellers.labellersNumberSearchCaptionValue : 
                                         (
                                          ((string)HttpContext.Current.Session["whichMenu"] == "systemSettingsMachine") ? WorldViewNet.system.settings.Machine.machineNumberSearchCaptionValue : 
                                          (
                                           ((string)HttpContext.Current.Session["whichMenu"] == "systemSystemFields") ? WorldViewNet.system.SystemFields.systemFieldsNumberSearchCaptionValue :
                                           (
                                            ((string)HttpContext.Current.Session["whichMenu"] == "systemCategories") ? WorldViewNet.system.Categories.categoriesNumberSearchCaptionValue : 
                                            (
                                             ((string)HttpContext.Current.Session["whichMenu"] == "systemDateFormats") ? WorldViewNet.system.DateFormats.dateFormatsNumberSearchCaptionValue : 
                                             (
                                              ((string)HttpContext.Current.Session["whichMenu"] == "systemTimeFormats") ? WorldViewNet.system.TimeFormats.timeFormatsNumberSearchCaptionValue : "nothing"
                                             ))))))))))))))))))))))))))))%>' />
\$\endgroup\$
1
\$\begingroup\$

Consider a Dictionary whose keys are your test strings, and whose values are your results.

e.g.

var sessionTypes = new Dictionary<string, string>()
sessionTypes.Add(
    "programmingLabels",
    (string)HttpContext.Current.Session["labelsNumberSearchCaptionValue"]);

//etc...

Then your code above becomes:

select name="searchTypeSelect" id="searchTypeSelect" size='1' class='searchUtilityMenu' >
    <option value='<%#
        sessionTypes.ContainsKey((string)HttpContext.Current.Session["whichMenu"]) ?
            sessionTypes[(string)HttpContext.Current.Session["whichMenu"]] : 
            "nothing";
    %>' />
\$\endgroup\$
1
\$\begingroup\$

I'd put that whole bit in a method that uses a switch, and call that. Something like:

<option value='<%# GetOption() %>' />

The method in code-behind:

public string GetOption()
{
    switch((string)HttpContext.Current.Session["whichMenu"])
    {
        case "programmingPLUSearch":
            return (string)HttpContext.Current.Session["pluNumberSearchCaptionValue"];
        case "programmingLabels":
            return (string)HttpContext.Current.Session["labelsNumberSearchCaptionValue"];
        // etc etc
        default:
            return "nothing";
    }
}

Nick Udell's suggestion is also a good idea to also implement next, but I'd use TryGetValue instead of ContainsKey. UPDATE: Actually, perhaps not. I'm thinking that these session/other variables might get updated often, and then such a dictionary would contain stale data. Of course you could maintain it, but then you basically would be doing twice the work.

I'd also put all these names of keys in a static class as public const string and use those instead of retyping/copy-pasting them when you need them in code.

\$\endgroup\$
  • \$\begingroup\$ If the variables are updated often, you could alter the value type of the dictionary to be an action that acquires the correct value, but I agree that at that point it is basically a switch statement anyway (although one that could be generated dynamically, perhaps). \$\endgroup\$ – Nick Udell Oct 30 '14 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.