Use BitArray collection as Flag : BitArray « Collections Data Structure « C# / C Sharp






Use BitArray collection as Flag

 

using System;
using System.Collections;

public class Starter {
    public static void Main() {
        Hashtable employees = new Hashtable();
        employees.Add("A100", new Employee("Ben", true, false, true));
        employees.Add("V100", new Employee("Valerie", false, false, true));
        Participation((Employee)employees["A100"]);
        Participation((Employee)employees["V100"]);
    }

    public static void Participation(Employee person) {
        Console.WriteLine(person.Name + ":");
        if (person.InProfitSharing) {
            Console.WriteLine(" Participating in Profit Sharing");
        }
        if (person.InHealthPlan) {
            Console.WriteLine(" Participating in Health Plan");
        }
        if (person.InCreditUnion) {
            Console.WriteLine(" Participating in Credit Union");
        }
    }
}

public class Employee {

    public Employee(string emplName) {
        propName = emplName;
        eflags.SetAll(true);
    }

    public Employee(string emplName,
                    bool profitSharing,
                    bool healthPlan,
                    bool creditUnion) {
        propName = emplName;
        InProfitSharing = profitSharing;
        InHealthPlan = healthPlan;

        InCreditUnion = creditUnion;
    }

    private BitArray eflags = new BitArray(3);

    public bool InProfitSharing {
        set {
            eflags.Set(0, value);
        }
        get {
            return eflags.Get(0);
        }
    }

    public bool InHealthPlan {
        set {
            eflags.Set(1, value);
        }
        get {
            return eflags.Get(1);
        }
    }

    public bool InCreditUnion {
        set {
            eflags.Set(2, value);
        }
        get {
            return eflags.Get(2);
        }
    }

    private string propName;
    public string Name {
        get {
            return propName;
        }
    }
}
           
         
  








Related examples in the same category

1.Demonstrate BitArrayDemonstrate BitArray
2.illustrates the use of a BitArrayillustrates the use of a BitArray
3.illustrates the use of BitArray methodsillustrates the use of BitArray methods
4.BitArray Class manages a compact array of bit values
5.Displays the properties and values of the BitArrays
6.BitArray.CopyTo copies the entire BitArray to a compatible one-dimensional Array
7.Gets the value of the bit at a specific position in the BitArray.
8.Inverts all the bit values in the current BitArray
9.Performs the bitwise OR operation
10.Performing OR between BitArray instances of different sizes returns an exception
11.Performs bitwise exclusive OR operation