We help IT Professionals succeed at work.

c#, case, if

ITsolutionWizard
on
127 Views
Last Modified: 2017-04-17
Below codes in c#. how can I use CASE instead?
The goal is to shorten the codes....

Thanks
DropDownList selectedInsuranceProductType = (DropDownList)InsuranceTypesList.FindControl("insuranceProductsList");
            if (selectedInsuranceProductType.SelectedValue == "Health")
            {
                panelHealth.Visible = true;
            }
            else if (selectedInsuranceProductType.SelectedValue == "Life")
            {
                panelLife.Visible = true;
            }
            else if (selectedInsuranceProductType.SelectedValue == "Accident")
            {
                panelAccident.Visible = true;
            }
            else if (selectedInsuranceProductType.SelectedValue == "Dental")
            {
                panelDental.Visible = true;
            }
            else
            {
                panelHealth.Visible = false;
                panelLife.Visible = false;
            }

Open in new window

Comment
Watch Question

Nitin SontakkeDeveloper
CERTIFIED EXPERT

Commented:
How about...

    panelHealth.Visible = selectedInsuranceProductType.SelectedValue == "Health";
    panelLife.Visible = selectedInsuranceProductType.SelectedValue == "Life";
    panelAccident.Visible = selectedInsuranceProductType.SelectedValue == "Accident";
    panelDental.Visible = selectedInsuranceProductType.SelectedValue == "Dental";

Open in new window

Author

Commented:
missing below
else
            {
                panelHealth.Visible = false;
                panelLife.Visible = false;
            }
Developer
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Nitin SontakkeDeveloper
CERTIFIED EXPERT

Commented:
Furthermore one of the commandments states that "Thou shalt not access object properties often".

If we are to abide by it:

    String insuranceProductType = selectedInsuranceProductType.SelectedValue;
    panelHealth.Visible = insuranceProductType == "Health";
    panelLife.Visible = insuranceProductType == "Life";
    panelAccident.Visible = insuranceProductType == "Accident";
    panelDental.Visible = insuranceProductType == "Dental";

Open in new window

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.