Check item Exists in List Item in Console Application using C#

Last Reply one year ago By kalpesh

Posted one year ago

I have a C# test coming up and I found one of the code question about get/set properties for filtering True/false of the values "vanilla", "mint", "chocolate', "red velvet".

I would like to know what the solution for the code below that will give the output result.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace ConsoleApps
{
    class Program
    {
       

        class Filter
        {
            //Member variables go here!
                      
            public string vanilla;   //???
            public string mint;      //???
            public string chocolate;     //???
            public string red velvet;      //???
           
             ///<summary>
            /// Creates and empty filter
            /// </summary>
            public Filter()
            {
               //???
            }
         }

        ///<summary>
        /// Gets or set whether this filter includes the added values.
        /// /// Defaults to false
        /// </summary>

        public bool Includes
        {
            get { return this.Includes; }      //???
            set {this.Includes =  value; }    //???
        }

        ///<summary>
        /// Gets or set whether this filter excludes the add values.
        /// Defaults to false
        /// </summary>
                  
        public bool Excludes
        {
            get { return this.Excludes; }    //???
            set { this.Excludes = value; }  //???
        }

        ///<summary>
        /// Adds values to the filter
        /// </summary>
        /// <param name="value"> identifier to be included/excluded (based on the property)</param>

     public void Add(string value)
             {
           //???
        }

        ///<summary>
        ///Tests whether a given value passes the filter
        /// </summary>
        /// <param name="value"> the value to be tested</param> 
        /// <returns> true if the value is present (Includes == true) or if the value (Excludes == true); false otherwise </returns>

        public bool Passes(string value)
              { 
               //???
          }

        static void Test(Filter filter, string value)
          {
            Console.WriteLine(value + " -- " + filter.Passes(value));
          }

        static void RunFilterDriver()
          {
              Filter filter = new Filter();
              filter.add("vanilla");
              filter.add("mint");
              filter.add("chocolate");

              filter.Includes = true;
              Test(filter, "mint");     //true
              Test(filter, "red velvet");  //false

              filter.Excludes = true;
              Test(filter, "mint");   //false
              Test(filter, "red velvet");  //true
          }

        static void Main(string[] args)
          {
               RunFilterDriver();
               Console.ReadLine();

        }

    }
}

Your help is much appreciated.  Thanks.

Posted one year ago

Your provided code is not correct there are some build error. Refer the below sample code. Also the property with the name is not correct if you need to pass the string value in class object then you just need one string variable and need to add that variable each time in List of the class object not in single object. also for to check true/false condition you just need to specify only one Boolean property not need to use two different property just set it as true when you need to check is filter value exist set it to false when filter value need to exclude from the checking.

Refer the below sample code for your reference and implement it as per your code logic.

C#

class Program
{
    class Filter
    {
        /// <summary>
        /// ItemValue 
        /// </summary>
        public string ItemValue;
    }

    ///<summary>
    /// Gets or set whether this filter includes the added values.
    /// /// Defaults to false
    /// </summary>
    public static bool FilterIncludes;

    static Filter AddFilterObject(string value)
    {
        Filter filter = new Filter();
        filter.ItemValue = value;
        return filter;
    }

    static void RunFilterDriver()
    {
        List<Filter> ListFilter = new List<Filter>();
        ListFilter.Add(AddFilterObject("vanilla"));
        ListFilter.Add(AddFilterObject("Mint"));
        FilterIncludes = true;
        FilterTest("vanilla", ListFilter);     //true
        FilterTest("red velvet", ListFilter);  //false
        FilterTest("Mint", ListFilter);  //true
        FilterIncludes = false;
        FilterTest("vanilla", ListFilter);   //false
        FilterTest("red velvet", ListFilter);  //true
        FilterTest("Mint", ListFilter);  //false
    }

    static void FilterTest(string value, List<Filter> ListFilter)
    {
        bool IncludeorExclude = false;
        foreach (Filter item in ListFilter)
        {
            if (item.ItemValue.ToUpper() == value.ToUpper())
            {
                IncludeorExclude = FilterIncludes;
                break;
            }
            else { IncludeorExclude = !FilterIncludes; }
        }
        string IncludedOrExcuded = IncludeorExclude ? "True" : "False";
        Console.WriteLine(value + " -- " + IncludedOrExcuded);
    }

    static void Main(string[] args)
    {
        RunFilterDriver();

        Console.ReadLine();
    }
}

Screenshot