I'm looking for a way to get a subset of a given enum to avoid repetition and use same enum type while comparing.
I found this question Enum subset or subgroup in C# and like the idea of using static class. Whoever, It doesn't allow to iterate over all elements in the class. So I added GetValues method.
Edit:
Here is my full code:
public partial class Devices : Form
{
public Devices()
{
InitializeComponent();
// Here, I need to iterate over StatusEnum items
foreach (var item in Enum.GetValues(typeof(StatusEnum)))
{
checkedListBoxParent.Items.Add(item);
}
// and iterate over ValidStatusEnum items
foreach (var item in ValidStatusEnum.GetValues())
{
checkedListBoxChild.Items.Add(item);
}
}
private void On_MouseUp(object sender, MouseEventArgs e)
{
// Here, I need to compare StatusEnum and ValidStatusEnum
if (Equals(checkedListBoxParent.CheckedItems.Cast<StatusEnum>().ToList(),
checkedListBoxChild.CheckedItems.Cast<StatusEnum>().ToList()))
{
MessageBox.Show("Parent and child are similar");
}
}
private bool Equals(List<StatusEnum> list1, List<StatusEnum> list2)
{
if (list1.Count != list2.Count)
return false;
for (int i = 0; i < list1.Count; i++)
{
if (list1[i] != list2[i])
return false;
}
return true;
}
private bool Equals(StatusEnum status, StatusEnum validStatus)
{
return status == validStatus;
}
}
public enum StatusEnum { Unknown, Known, NotAvailable, Available };
internal static class ValidStatusEnum
{
public const StatusEnum Known = StatusEnum.Known;
public const StatusEnum Available = StatusEnum.Available;
public static StatusEnum[] GetValues()
{
return new StatusEnum[] { Known, Available };
}
}
}
So basically, in my winforms application, I have two CheckedListBoxes (Parent and Child) populated from StatusEnum
Enum and ValidStatusEnum
static class.
So, I need to iterate over enums to populate Checkboxes and I need to compare StatusEnum
and ValidStatusEnum
to display a message to user in case I have the same selection in both checkboxes.
Any thoughts about the design and the code?
StatusEnum
and will find out if it is part ofValidStatusEnum
\$\endgroup\$