Determines whether the NameValueCollection contains a specific value. - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:ICollection

Description

Determines whether the NameValueCollection contains a specific value.

Demo Code


using System.IO;/*  w  w w  .  j a  va  2 s. c o  m*/
using System;

public class Main{
        /// <summary>
        /// Determines whether the NameValueCollection contains a specific value.
        /// </summary>
        /// <param name="d">The dictionary to check for the value.</param>
        /// <param name="obj">The object to locate in the SortedList.</param>
        /// <returns>Returns true if the value is contained in the NameValueCollection, false otherwise.</returns>
        public static bool ContainsValue(System.Collections.Specialized.NameValueCollection d, Object obj)
        {
            bool contained = false;
            Type type = d.GetType();

            for (int i = 0; i < d.Count && !contained; i++)
            {
                String[] values = d.GetValues(i);
                if (values != null)
                {
                    foreach (String val in values)
                    {
                        if (val.Equals(obj))
                        {
                            contained = true;
                            break;
                        }
                    }
                }
            }
            return contained;
        }
        /// <summary>
        /// Determines whether the SortedList contains a specific value.
        /// </summary>
        /// <param name="d">The dictionary to check for the value.</param>
        /// <param name="obj">The object to locate in the SortedList.</param>
        /// <returns>Returns true if the value is contained in the SortedList, false otherwise.</returns>
        public static bool ContainsValue(System.Collections.IDictionary d, Object obj)
        {
            bool contained = false;
            Type type = d.GetType();

            //Classes that implement the SortedList class
            if (type == Type.GetType("System.Collections.SortedList"))
            {
                contained = (bool)((System.Collections.SortedList)d).ContainsValue(obj);
            }
            //Classes that implement the Hashtable class
            else if (type == Type.GetType("System.Collections.Hashtable"))
            {
                contained = (bool)((System.Collections.Hashtable)d).ContainsValue(obj);
            }
            else
            {
                //Reflection. Invoke "containsValue" method for proprietary classes
                try
                {
                    System.Reflection.MethodInfo method = type.GetMethod("containsValue");
                    contained = (bool)method.Invoke(d, new Object[] { obj });
                }
                catch (System.Reflection.TargetInvocationException e)
                {
                    throw e;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            return contained;
        }
}

Related Tutorials