-1

Here i have used if , else if condition to show an error message and make some label visible and invisible, but i am trying to use ternary operator to do so but i am quite unfamiliar with ternery operator and unable to use it for all condition i have in my if else code.
So any help with my code will be highly appreciated. Thank you.

Below is my code

        catch (Exception ex)
        {
            if (ex.Message == "Parent Menu Title Required")
            {
                metroLabel4.Visible = true;
                metroLabel5.Visible = false;
                metroLabel6.Visible = false;
                metroLabel4.Text = ex.Message;
            }
            else if (ex.Message == "Menu Title Required")
            {
                metroLabel4.Visible = false;
                metroLabel5.Visible = true;
                metroLabel6.Visible = false;
                metroLabel5.Text = ex.Message;
            }
            else if (ex.Message == "Form Name Required")
            {
                metroLabel4.Visible = false;
                metroLabel5.Visible = false;
                metroLabel6.Visible = true;
                metroLabel6.Text = ex.Message;
            }
            else
            {
                metroLabel4.Visible = true;
                metroLabel5.Visible = true;
                metroLabel6.Visible = true;
                metroLabel4.Text = "Parent Menu Title Required";                  
                metroLabel5.Text = "Menu Title Required";
                metroLabel6.Text = "Form Name Required";
            }
        }
CarrotCrop
  • 73
  • 1
  • 5
  • 12

2 Answers2

2

The ternary operator is not a good fit for your problem. It is used to set the value of one variable to one of two values, based on a predicate:

var thingToSet = predicateA ? 
                 ifPredicateAIsTrue : 
                 ifPredicateAIsFalse;

This is the same as:

if (predicateA)
    thingToSet = ifPredicateAIsTrue;
else
    thingToSet = ifPredicateAIsFalse;

To nest ternary expressions, place a new ternary expression in the value to set:

var otherThingToSet = predicateB ? (
                            predicateC ? 
                            ifPredicateCIsTrue : 
                            ifPredicateCIsFalse
                        ) : (
                            predicateD ? 
                            ifPredicateDIsTrue : 
                            ifPredicateDIsFalse
                        );

This is equivalent to:

if (predicateB)
{
    if (predicateC)
        otherThingToSet = ifPredicateCIsTrue;
    else
        otherThingToSet = ifPredicateCIsFalse;
}
else
{
    if (predicateD)
        otherThingToSet = ifPredicateDIsTrue;
    else
        otherThingToSet = ifPredicateDIsFalse;
}

As you can see, this is not really a good fit for your problem, as you're trying to set the value of several variables, based on the exception message.

A better fit for your problem would be a switch statement:

switch (ex.Message)
{
    case "Parent Menu Title Required":
        metroLabel4.Visible = true;
        metroLabel5.Visible = false;
        metroLabel6.Visible = false;
        metroLabel4.Text = ex.Message;
        break;
    case "Menu Title Required":
        metroLabel4.Visible = false;
        metroLabel5.Visible = true;
        metroLabel6.Visible = false;
        metroLabel5.Text = ex.Message;
        break;
    case "Form Name Required":
        metroLabel4.Visible = false;
        metroLabel5.Visible = false;
        metroLabel6.Visible = true;
        metroLabel6.Text = ex.Message;
        break;
    default:
        metroLabel4.Visible = true;
        metroLabel5.Visible = true;
        metroLabel6.Visible = true;
        metroLabel4.Text = "Parent Menu Title Required";
        metroLabel5.Text = "Menu Title Required";
        metroLabel6.Text = "Form Name Required";
        break;
}
Erresen
  • 1,923
  • 1
  • 22
  • 41
1

Your code is equivalent to:

const string ParMnuTitReq ="Parent Menu Title Required";
const string MnuTitReq ="Menu Title Required";
const string FrmNamReq ="Form Name Required";

string m = ex.Message;
metroLabel4.Visible =  m != MnuTitReq && m != FrmNamReq;
metroLabel5.Visible = m != ParMnuTitReq && m != FrmNamReq;
metroLabel6.Visible = m != ParMnuTitReq && m != MnuTitReq;

// This can be done in the form designer
metroLabel4.Text = ParMnuTitReq;
metroLabel5.Text = MnuTitReq;
metroLabel6.Text = FrmNamReq;

You don't need ternary expressions. Instead, you can combine logical expressions. In the case of the Visible property which is of type bool, you can directly assign the result of the logical expression.

You can always assign the same text to the labels, as they won't be visible if the text does not apply. You could even drop the 3 last code lines and instead assign the text in the form designer. This reduces your original 23 lines of code (not counting lines with braces only) to 7.


Nested or chained ternary expressions can be used if you must be able to assign more than 2 different values.

string t = x == 1 ? "case 1" : x == 2 ? "case 2" : x == 3 ? "case 3" : "other case";

Is equivalent to

string t;
if (x == 1) {
    t = "case 1";
} else if (x == 2) {
    t = "case 2";
} else if (x == 3) {
    t = "case 3";
} else {
    t = "other case";
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188