In some scenarios, when I pass Enum to a method, I need to handle whether this is the only Enum value or, otherwise, a combination of flags, for this purpose I wrote this simple extension:
Vb.Net:
<Extension>
Public Function FlagCount(ByVal sender As System.[Enum]) As Integer
Return sender.ToString().Split(","c).Count()
End Function
C # (online translation):
[Extension()]
public int FlagCount(this System.Enum sender) {
return sender.ToString().Split(',').Count();
}
Usage example:
Vb.Net:
Dim flags As FileAttributes = (FileAttributes.Archive Or FileAttributes.Compressed)
Dim count As Integer = flags.FlagCount()
MessageBox.Show(flagCount.ToString())
C # (online translation):
FileAttributes flags = (FileAttributes.Archive | FileAttributes.Compressed);
int count = flags.FlagCount();
MessageBox.Show(flagCount.ToString());
I would just like to ask if there is a more direct and efficient way that what I am doing now is to avoid the combination of flags as a string, then split it.
source
share