0

I have a enum which contains 3 values for 3 checkboxes:

public enum Str
{
    Test = 1,
    Exam = 2,
    Mark = 4
}

Imagine these are checkboxes. If I select any of them it works fine but when I select multiple checkboxes are selected, Enum values are getting added.

When I check Test and Mark Enum value is 5 and when I select Test and Exam the result is 3 I even tried Type casting

 string sVal = "checkbox Value";
 bool ival = int.TryParse(sValue,out iVal);
 if(iVal)
 {
   int iValue = int.Parse(sValue)
    str s = (str)iValue;
 }

again "s" returns the added value not the enum types how to solve this?

chaliasos
  • 9,659
  • 7
  • 50
  • 87
Aravind
  • 177
  • 1
  • 3
  • 13

5 Answers5

1

I think what you're are looking for is the Flags attribute: http://msdn.microsoft.com/en-gb/library/system.flagsattribute.aspx

Matt Whetton
  • 6,616
  • 5
  • 36
  • 57
1

You do want the value to be the addition of 1 and 4. Here's how to test your values:

public enum Str
{
    Test = 1,
    Exam = 2,
    Mark = 4
}

private static void Main()
{
    Str test = (Str)5;  // Same as  test = Str.Test | Str.Mark;

    if ((test & Str.Test) == Str.Test)
    {
        Console.WriteLine("Test");
    }

    if ((test & Str.Exam) == Str.Exam)
    {
        Console.WriteLine("Exam");
    }

    if ((test & Str.Mark) == Str.Mark)
    {
        Console.WriteLine("Mark");
    }

    Console.Read();
}

The Flag attribute should be used, so other people know your enum should be used with bitwise operations. But this attribute itself does nothing (expect maybe modifying the .ToString() result).

ken2k
  • 48,145
  • 10
  • 116
  • 176
0

You will need to do several things for this to work for you.

  1. Set the [Flags] attribute on the enum. It will work without it, but it's a nice thing to have, even if just for documentation purposes.

    [Flags]
    public enum Str
    {
      None = 0
      Test = 1,
      Exam = 2,
      Mark = 4
    }
    
  2. To set the Enum you'll need to loop the selected checkboxes and set the values, something along the lines of:

    Str value = Str.None;
    if (chkTest.Checked)
       value = value | Str.Test;
    if (chkExam.Checked)
       value = value | Str.Exam;
    if (chkMark.Checked)
       value = value | Str.Mark;
    

    After this runs, if, lets say, Test and exam are checked, the value will be:

    (int) value       =>  3
    value.ToString()  => "Str.Test|Str.Exam".
    
  3. To check if a enum value has a specific flag, you could do:

    Str value = ....
    if (value.HasFlag(Str.Test))
       // it has test selected 
    else
       // it does not have test selected
    

    or you could do

    Str value = ....
    if (value & Str.Test == Str.Test)
       // it has test selected 
    else
       // it does not have test selected
    
SWeko
  • 30,434
  • 10
  • 71
  • 106
0
         if((EnumVal & Str.Exam) ==Str.Exam)|| EnumVal == Str.Exam) 

Solved.....

Aravind
  • 177
  • 1
  • 3
  • 13
0

You may not use Flags attribute. But your enum values should be pow of 2.

Int values of your enums:

var values = Enum.GetValues(typeof(Str)).Cast<int>().Where(x => (x & iVal) != 0).ToList()

Then:

values.Select(x => list[(int)Math.Log(x, 2)])

list is list of your checkboxes which you can iterate and set checked.

var list = new List<CheckBox>
           {
               firstCheckBox,
               secondCheckBox,
               thirdCheckBox,
           };
Alexander Balte
  • 900
  • 5
  • 11