Glory Raj
Glory Raj

Reputation: 17701

how to reduce the code with this enum lists

I have these if-else statements that I am not able to refactor. I have used them for doing validations on server-side using asp.net.

Would anyone please suggest ways to reduce these statements? Here, validation fields and validation types are enum lists.

else if (CheckNextItem(ddlBr1Type.SelectedValue.ToString(), ddlBr2Type.SelectedValue.ToString(), ValidationFields.FO.ToString(), ValidationTypes.P.ToString()))
{
   BrkTypeValidator2.ErrorMessage = "'TOP0618 -Invalid combination of Bracket IDs'";
   args.IsValid = false;
}
else if (CheckNextItem(ddlBr1Type.SelectedValue.ToString(), ddlBr2Type.SelectedValue.ToString(), ValidationFields.FW.ToString(), ValidationTypes.P.ToString()))
{
   BrkTypeValidator2.ErrorMessage = "'TOP0618 -Invalid combination of Bracket IDs'";
   args.IsValid = false;
}
else if (CheckNextItem(ddlBr1Type.SelectedValue.ToString(), ddlBr2Type.SelectedValue.ToString(), ValidationFields.UF.ToString(), ValidationTypes.P.ToString()))
{
     BrkTypeValidator2.ErrorMessage = "'TOP0618 -Invalid combination of Bracket IDs'";
     args.IsValid = false;
}
else if (CheckNextItem(ddlBr1Type.SelectedValue.ToString(), ddlBr2Type.SelectedValue.ToString(), ValidationTypes.O.ToString(), ValidationTypes.P.ToString()))
{
      BrkTypeValidator2.ErrorMessage = "'TOP0618 -Invalid combination of Bracket IDs'";
      args.IsValid = false;
}
else if (CheckNextItem(ddlBr1Type.SelectedValue.ToString(), ddlBr2Type.SelectedValue.ToString(), ValidationTypes.W.ToString(), ValidationTypes.P.ToString()))
{
      BrkTypeValidator2.ErrorMessage = "'TOP0618 -Invalid combination of Bracket IDs'";
      args.IsValid = false;

}
else if (CheckNextItem(ddlBr1Type.SelectedValue.ToString(), ddlBr2Type.SelectedValue.ToString(), ValidationTypes.P.ToString(), ValidationTypes.C.ToString()))
{
      BrkTypeValidator2.ErrorMessage = "'TOP0618 -Invalid combination of Bracket IDs'";
      args.IsValid = false;

}
else if (CheckNextItem(ddlBr1Type.SelectedValue.ToString(), ddlBr2Type.SelectedValue.ToString(), ValidationTypes.C.ToString(), ValidationTypes.U.ToString()))
{
      BrkTypeValidator2.ErrorMessage = "'TOP0618 -Invalid combination of Bracket IDs'";
      args.IsValid = false;

}

This is method for Checknextitem:

 public static bool CheckNextItem(string Compareitem1, string comnpareitem2, string items1, string items2)
  {

        var listContains = Compareitem1 == items1 && comnpareitem2 != items2;
        return listContains;

  }

if i use this code

 var invalidComparisons = new ValidationTypes[][] {
                      new[] { ValidationFields.FO, ValidationTypes.P },
                      new[] { ValidationFields.FW, ValidationTypes.P },
                      new[] { ValidationFields.UF, ValidationTypes.O },
                      new[] { ValidationTypes.O, ValidationTypes.P },
                      new[] { ValidationTypes.W, ValidationTypes.P },
                      new[] { ValidationTypes.P, ValidationTypes.C },
                      new[] { ValidationTypes.C, ValidationTypes.U },
                   };

i am getting compile time error : "No Best Type found for implicitly- typed array".

Upvotes: 1

Views: 65

Answers (1)

p.s.w.g
p.s.w.g

Reputation: 149020

Use a list / array to store which validation types to compare:

var invalidComparisons = new string[][] {
        new[] { ValidationFields.FO.ToString(), ValidationTypes.P.ToString() },
        new[] { ValidationFields.FW.ToString(), ValidationTypes.P.ToString() },
        new[] { ValidationFields.UF.ToString(), ValidationTypes.O.ToString() },
        new[] { ValidationTypes.O.ToString(), ValidationTypes.P.ToString() },
        new[] { ValidationTypes.W.ToString(), ValidationTypes.P.ToString() },
        new[] { ValidationTypes.P.ToString(), ValidationTypes.C.ToString() },
        new[] { ValidationTypes.C.ToString(), ValidationTypes.U.ToString() },
};

if (invalidComparisons.Any(x => CheckNextItem(ddlBr1Type.SelectedValue.ToString(), ddlBr2Type.SelectedValue.ToString(), x[0], x[1]))
{
    BrkTypeValidator2.ErrorMessage = "'TOP0618 -Invalid combination of Bracket IDs'";
    args.IsValid = false;
}

Upvotes: 5

Related Questions