Using IList Over Multiple Or Conditions in IF statement

Introduction

Today I got a scenario where I need to check multiple conditions in a single IF statement where the values are coming from a config file.

First I did this thing using IF statement and inside this IF I have given 2-3 OR conditions, but suddenly one thing stroke my head that now there is 2-3 conditions but what if it goes on increasing. Then I thought about IList. And find out a way to shut it out simply.

Here in this article we will go through a simple example how to overcome this problem of checking multiple values with OR conditions in IF statement.



How to Use IList Over Multiple Or Conditions

Coming to the background let me explain this with a simple example. Please download the above code sample for a better demonstration.

Lets say we have 5 keys in our web config file which we need to check in an IF condition to perform certain tasks. Our web config file will look like this.
   <!-- Here is the key for Nested If Demo-->

    <add key="DCEE" value="Demo_CentralEasternEurope_Region"/>
    <add key="DME" value="Demo_MiddleEastArabic_Region"/>
    <add key="DRU" value="Demo_Russia_Region"/>
    <add key="DSA" value="Demo_SouthAfrica_Region"/>
    <add key="DTI" value="Demo_TurkeyIsrael_Region"/>

    <!--Key section Ends -->
    
  </appSettings>

So if we need to check these keys in a single IF statement then we need to do something like this
protected void Button2_Click(object sender, EventArgs e)
        {
            string regions = string.Empty;
            string enteredText = string.Empty;
            enteredText = TextBox1.Text.Trim();           

            // check if the Data entered in Text Box is in Config File then show a text saying it exists.

            if (enteredText == ConfigurationManager.AppSettings["DCEE"]
                || enteredText == ConfigurationManager.AppSettings["DME"]
                  || enteredText == ConfigurationManager.AppSettings["DRU"]
                    || enteredText == ConfigurationManager.AppSettings["DSA"]
                      || enteredText == ConfigurationManager.AppSettings["DTI"])
            {
                Label1.Text = "The Region Name Entered, exists in Config....." ;
                Label1.CssClass = "Success";
                Label2.Text = "Method Used : IF Condition";
                Label2.CssClass = "Success";
            }
            else
            {
                Label1.Text = "The Region Name Entered, does not exist in Config.....";
                Label1.CssClass = "Failed";
                Label2.Text = "Method Used : IF Condition";
                Label2.CssClass = "Failed";
            }

        }
In the above button click event I am checking the config key values against a textbox value entered by the user.

Just think about the IF statement. How it looks ? If the key values goes on increasing then how does it look like and how may OR conditions we need to put in a single IF statement.

So to resolve this problem and to make our code a bit modular and reusable IList came to picture. We can solve this problem using IList.

Now for using IList we need to create a single key in the web config as below.


  <appSettings>
    <!-- Here is the key for IList Demo-->    
    <add key="DemoRegions" value="Demo_CentralEasternEurope_Region;Demo_MiddleEastArabic_Region;Demo_SouthAfrica_Region;Demo_Russia_Region;Demo_TurkeyIsrael_Region"/> 
    <!--Key section Ends -->    
  </appSettings>
Now see the bellow method which interacts with this key values and returns you a list.

 private IList<string> GetListDataFromConfig(string regionsToSplit)
        {
            IList<string> regions = new List<string>();

            regions = regionsToSplit.Split(";".ToCharArray()).ToList();
            return regions;
        }

Simply the above method takes a string variable which contains the key values separated by a " ; " and returns a list of the key values. Now just call this above method like below.
  protected void Button1_Click(object sender, EventArgs e)
        {
            string regions = string.Empty;

            // Get the key values stored in the confing file in the key name "DemoRegions"

            if (ConfigurationManager.AppSettings["DemoRegions"] != string.Empty)
            {
                regions = ConfigurationManager.AppSettings["DemoRegions"];
            }

            // check if the Data entered in Text Box is in Config File then show a text saying it exists.

            if (GetListDataFromConfig(regions).Contains(TextBox1.Text.Trim()))
            {
                Label1.Text = "The Region Name Entered, exists in Config.....";
                Label1.CssClass = "Success";
                Label2.Text = "Method Used : IList Condition";
                Label2.CssClass = "Success";
            }
            else
            {
                Label1.Text = "The Region Name Entered, does not exist in Config.....";
                Label1.CssClass = "Failed";
                Label2.Text = "Method Used : IList Condition";
                Label2.CssClass = "Failed";
            }
        }

Here in the above button click event we are calling the IList method from an IF condition with a parameter that hold the key value from the config.

And in the IF condition  GetListDataFromConfig(regions).Contains(TextBox1.Text.Trim())
this method checks if the entered text in the textbox is present in the config key values or not.

Just look at the two button click events one with multiple OR conditions and one with a simple IList method implementation, and say which one is more usable and looks modular. 

For a better demo and understanding you can download the sample code file from above download link.


Happy Coding...

No comments:

Post a Comment