Pass multiple enum to methods and get their values - c#

Please consider this code:
public enum Status
{
S1 = 1,
S2 = 2,
S3 = 3,
S4 = 4
}
I know I can pass multiple enum using | oerator to a method:
public void DoWork(Status S)
{
}
...
DoWork(Status.S1 | Status.S2);
Now In DoWork Method I want to get values of passed enums. For Example In above code I want to get {1, 2}. How I cas do this?
thanks

Here are few steps to follow to get flagged enum :
Use 2 exp (n) integer (1, 2, 4, 8, 16, 32, ...)
to define your enum. Why ? : Actually each active state of your
enum will take a single bit of a 32 bits integer.
Add the Flags attribute.
Then,
[Flags]
public enum Status
{
S1 = 1,
S2 = 2,
S3 = 4,
S4 = 8
}
You can use Enum.HasFlag to check if a specific status is active :
public void DoWork(Status s)
{
var statusResult = Enum.GetValues(typeof(Status)).Where(v => s.HasFlag(v)).ToArray() ;
// StatusResult should now contains {1, 2}
}

Declare your parameters with the params tag, then you get an array of enums:
public void DoWork (params Status[] args) {
Console.WriteLine(args.Length);
}
Then you just pass them in as regular parameters:
DoWork(Status.S1, Status.S2);
This doesn't require changes to your enums, and easily copes with additional values. The flags solution above may work for you as well - depends on your requirements.

If this is the Integer values of your enums that you want you can use this :
//declare your enum as byte
public enum Status : byte
{...}
**EDITED**
//then cast it to int to use the value
DoWork((int)Status.S1 , (int)Status.S2);
//change doWork to accept int[]
public void DoWork (params int[] args)

Related

How to implement a range of constant predefined "states" of a property?

I have a property LegalCaseStatus. My intention is to make the property to accept a predefined range of approved values. The range must be visible and unchanged throughtout my program. Here's an example of the list:
Plaintiff
Defendant
Third Party
Debitor
Creditor
Petitioner
So, the best way I could think of is to declare a static class, and fill it with corresponding constants:
public static class Participants
{
public const byte
Piaintiff = 0,
Defendant = 1,
ThirdParty = 2,
Debitor= 3,
Creditor = 4,
Petitioner = 5;
}
so after using a namespace I could just do:
public byte LegalCaseStasus = Plaintiff;
the only problem is, since it's just a byte member it'll accept anything that is byte:
LegalCaseStatus = 99; // ok
LegalCaseStatus = SomeOtherByteConstant; // ok
How do I protect the member LegalCaseStatus? Is my solution generally correct?
You can use enums - An enum is a special "class" that represents a group of constants (unchangeable/read-only variables). Sounds like the thing you describe in your question:
public enum Participants
{
Piaintiff = 0,
Defendant = 1,
ThirdParty = 2,
Debitor = 3,
Creditor = 4,
Petitioner = 5
}
After the enum definition you can use it exactly the way you want to:
Participants LegalCaseStasus = Participants.ThirdParty;
LegalCaseStasus = 99; // ERROR
byte underlying_value = (byte)LegalCaseStasus; // value == 2
Note: The underlying value of an enum is int! When you cast to byte you need to make sure there are no predefined values that exceed the byte limit.

Union of enum members in C#

Let us say, I have an enum BasicType, which is defined as follows:
public enum ObjectType{
A = 1,
B = 2,
C = 3,
}
The BasicType identifies performs a ternary classification of any Object. Subsequently, I realized that the objects A and B need to be treated in a similar way as compared to C, so I defined another enum ObjectGroupType as follows :
public enum ObjectGroupType
{
AB = 1,
C = 2,
}
With the new enum, I am able to bucket objects of several known types as one. So, when I receive a stream of objects in various types, I actually identify whether they belong to AB or C type. Is there an elegant workaround for this? For instance, will I be able to assign the same enum values for A and B in the ObjectGroupType?:
Edit 1 : I am unable to find the resemblance to the question here
Edit 2 : Thank you Maurice for your constructive inputs -- taking cues from your answer, I came up with this redefined ObjectGroupType.
public enum ObjectGroupType
{
AB = ObjectType.A | ObjectType.B
C = 2,
}
Is this valid?
Essentially, when I process a stream of objects of type AB, I want to ascertain Type A or Type B objects. This is quite similar to a hierarchical two-level decision tree:
object
/ \
AB C
/\
A B
I apologize in advance if I misread your intent, but it almost sounds like you want to allow multiple different enum types to be acted on in your code based on the enum value. The good thing is that you can do that already with bitwise operations and enums.
Given an enum that looks like this:
[Flags]
enum ObjectType
{
A = 1,
B = 2,
C = 4,
D = 8
}
You can set a comparison value that is the bitwise OR of several values:
var allowedValues = ObjectType.A | ObjectType.C;
This works because the values in the enum act like bit fields under the covers.
When you run your code, you do a bitwise AND on the allowedValues variable and the test variable and see if it matches your test variable. If it does, then it is one of the values you want:
if ((test & allowed) == test) ...
Below is a working example using the enum above that shows you how it works.
void Main()
{
var allowed = ObjectType.A | ObjectType.C;
var values = new int [] { 1, 2, 4, 8 };
foreach (var i in values)
{
var test = (ObjectType)i;
if ((test & allowed) == test)
{
Console.WriteLine("Found a match: {0}", test);
}
else
{
Console.WriteLine("No match: {0}", test);
}
}
}
Best of luck!
Edit:
I found the answer of Maurice Reeves very good, I only want to bring some more info:
[Flags]
public enum ObjectType
{
None=0,
A = 1,
B = 2,
C = 4,
D = 8,
E = 16,
AorB=A|B,
BorCorD=B|C|D,
}
By using [Flags] attribute, you can create sets of enum items, which can help you establishing different business rules for each set.
In order to check if and item exist in a set you can do as follow:
public static bool IsAorB(this ObjectType item)
{
return ObjectType.AorB.HasFlag(item);
}
if you want to creat on the fly new set of items, you can do:
var newGroup=ObjectType.A | ObjectType.BorCorD;
if you want to apply some business rule to a set, except an item, you can do:
var newGroupExceptC =newGroup^=ObjectType.C;
Now if you check if element C exist in the set you will get false:
bool exist=newGroupExceptC.HasFlag(ObjectType.C) // =false
more info you can find here
You might use a int instead of an enum, use values that don't overlap when combined (i.e. values whose binary representation has only one bit on) and then perform a mask operation on the ObjectType of a parameter to determine if it is AB:
class SomeClass
{
public static class ObjectType
{
public const int A = 1;
public const int B = 2;
public const int C = 4;
public const int D = 8;
}
public int MyType;
public string Title;
static void Main(string[] args)
{
List<SomeClass> list = new List<SomeClass>()
{
new SomeClass() {Title ="I am of type A", MyType = ObjectType.A }
,new SomeClass() {Title ="I am of type B", MyType = ObjectType.B }
,new SomeClass() {Title ="I am of type AB", MyType = ObjectType.A | ObjectType.B }
};
list.ForEach(p => { if (p.MyType == (ObjectType.A | ObjectType.B)) Console.WriteLine(p.Title); });
}
}
The downside of this approach is losing strong-typing of Object Type, i.e. you can assign any value not just those you define in the ObjectType.

How to get enum order?

If I have enum:
public enum ImportState : byte
{
None = 0,
ImportedWithChanges = 44,
AwaitingApproval = 45,
Removing = 66,
Revalidating = 99,
};
How to get enum order?
For example:
GetOrder(ImportState.None)
Should return 1(first in order)
GetOrder(ImportState.AwaitingApproval )
Should return 3 (third in order)
here is the missing method GetOrder
public static int GetOrder(ImportState State)
{
return Enum.GetValues(typeof(ImportState)).Cast<ImportState>().Select((x, i) => new { item = x, index = i }).Single(x => x.item == State).index;
}
As other noticed, Enum.GetValues() returns the values of an enum sorted by value. Perhaps this isn't what you wanted... So, using a little reflection:
public class EnumOrder<TEnum> where TEnum : struct
{
private static readonly TEnum[] Values;
static EnumOrder()
{
var fields = typeof(Values).GetFields(BindingFlags.Static | BindingFlags.Public);
Values = Array.ConvertAll(fields, x => (TEnum)x.GetValue(null));
}
public static int IndexOf(TEnum value)
{
return Array.IndexOf(Values, value);
}
}
Example of use:
public enum Values
{
Foo = 10,
Bar = 1
}
int ix = EnumOrder<Values>.IndexOf(Values.Bar); // 1
Note that the C# specifications aren't clear if the "source code" ordering of an enum is maintained in the compiled program... At this time the C# compiler seems to maintain it, but there is no guarantee in the future...
The only two references I've found are:
Forward declarations are never needed in C# because, with very few exceptions, declaration order is insignificant
and
Declaration order for enum member declarations (§14.3) is significant when constant-expression values are omitted.
So as written, for the example I gave, the ordering is undefined and depends on the C# compiler!
Enumerating the enum values, casting to an IEnumerable, converting to a List. This it is a simple matter of using IndexOf().
Note that for this to work, the enum must be declared in increasing order.
namespace ConsoleApplication1
{
using System;
using System.Linq;
class Program
{
public enum ImportState : byte
{
None = 0,
ImportedWithChanges = 44,
AwaitingApproval = 45,
Removing = 66,
Revalidating = 99,
}
static void Main(string[] args)
{
Console.WriteLine(GetOrder(ImportState.None));
Console.WriteLine(GetOrder(ImportState.AwaitingApproval));
}
public static int GetOrder(ImportState state)
{
var enumValues = Enum.GetValues(typeof(ImportState)).Cast<ImportState>().ToList();
return enumValues.IndexOf(state) + 1; // +1 as the IndexOf() is zero-based
}
}
}
1
3
Press any key to continue . . .
Sth. like this?
int i = 0;
foreach (ImportState state in Enum.GetValues(typeof(ImportState)))
{
i++;
if (state == myState) return i;
}
However there is no real use for this, as enums do not provide an indexed enumeration in themselfes. They represent a value which is more what you´re probably after.
You can use this LINQ query:
int position = Enum.GetValues(typeof(ImportState)).Cast<ImportState>()
//.OrderBy(impState => (int)impState)
.TakeWhile(impState => impState != ImportState.None)
.Count() + 1;
It orders by the int-value of the enum-value, then it takes all until the searched value and counts them. I have omitted the OrderBy since Enum.GetValues automatically returns the order according to their int-value.
MSDN:
The elements of the array are sorted by the binary values of the
enumeration constants
Instead of using the order, you would be better to make better use of the flags. Consider the following
public enum ImportState : byte
{
None = 0,
ImportedWithChanges = 2,
AwaitingApproval = 4,
Removing = 6,
Revalidating = 8,
};
(double)state / Enum.GetValues(typeof(ImportState)).Cast<byte>().Max()
Example
Enums don't really have any sense of ordering, using the above probably still isn't perfect but it doesn't involve a made up order.
What about this Solution?
var result = from r in Enum.GetValues<ImportState>()
let expression =
r == ImportState.Revalidating
? 0
: r == ImportState.AwaitingApproval
? 1
: r == ImportState.Removing
? 2
: r == ImportState.ImportedWithChanges
? 3
: 4
orderby expression ascending
select r.ToString();
Console.WriteLine(string.Join(", ", result));
Output: Revalidating, AwaitingApproval, Removing, ImportedWithChanges, None

How do I get the assigned values for enum members?

The following code returns One, Two, Three, where I expect GetNames to do that and GetValues to return 2, 5, 10:
enum Nums
{
One = 2,
Two = 5,
Three = 10
}
class Program
{
static void Main()
{
var vals = Enum.GetValues(typeof(Nums));
}
}
What is up here? How do I get the values 2, 5, 10 from the type Nums?
cast it to an int like this:
int value = (int)Num;
You need to cast it to an int:
int val = (int)Nums.One;
Or to answer your question for getting all of the items in the enum:
IEnumerable<int> values = Enum.GetValues(typeof(Nums)).Cast<int>();
You can do it like this:
int[] vals = Enum
.GetValues(typeof(Nums)) // Thus far, this is your code
.Cast<int>() // Cast individual elements to int
.ToArray(); // Convert the result to array
Here is a demo on ideone.
I haven't fully wrapped my head around C# Enums yet, but the Enum.GetValues documentation has an example that shows that casting has some very interesting and powerful effects on them. http://msdn.microsoft.com/en-us/library/system.enum.getvalues(v=vs.110).aspx
using System;
enum SignMagnitude { Negative = -1, Zero = 0, Positive = 1 };
public class Example
{
public static void Main()
{
foreach (var value in Enum.GetValues(typeof(SignMagnitude))) {
Console.WriteLine("{0,3} 0x{0:X8} {1}",
(int) value, ((SignMagnitude) value));
} }
}
// The example displays the following output:
// 0 0x00000000 Zero
// 1 0x00000001 Positive
// -1 0xFFFFFFFF Negative

Is there a pattern or a method in C# to check if an (int 1,2,4,8,...) option is true or false

I like to write enum or integer to pass option to my methods. Is there a pattern or a method in C# to check if an (int 1,2,4,8,...) option is true or false. I think it should easily be possible via binary functions.
class Program
{
public enum Option
{
Option_A = 1,
Option_B = 2,
Option_C = 4,
Option_D = 8,
}
static void Main(string[] args)
{
int activeOption = 5; // Means I activeted the Option_A and Option_C
if (IsOption(activeOption, Option.Option_A)) { /*do work*/ }
if (IsOption(activeOption, Option.Option_B)) { /*do work*/ }
if (IsOption(activeOption, Option.Option_C)) { /*do work*/ }
if (IsOption(activeOption, Option.Option_D)) { /*do work*/ }
}
private static bool IsOption(int activeOption, Option option)
{
/*Evaluate if IsOption is true or false*/
throw new NotImplementedException();
}
}
EDIT
Am I limited the number of options that I can create like this?
Since your enum contains flags (or if you prefer, is a bitfield), you should add a FlagsAttribute to it:
[Flags]
public enum Option
{
Option_A = 1,
Option_B = 2,
Option_C = 4,
Option_D = 8,
}
And then, checking is typically done with the bitwise and operator. A cast will also be needed, because you are using an int variable.
if(((Option)activeOption & Option.Option_A) != Option.Option_A) //...
If you want to encapsulate this nastiness away, check out the article linked in Smudge202's answer. If you are running .NET 4, you don't even need to do that: check sehe's answer.
But you should really try using a variable of type Option directly, and combine the options with the bitwise or operator:
Option activeOption = Option.Option_A | Option.Option_C;
Of course using this scheme limits the number of options you can create. If you leave it as is, you can only create 32 different options, because an int (the default underlying type of an enum) has only 32-bits. If you use a long you can have 64 different options:
[Flags]
public enum Option : long
{
Option_A = 1,
Option_B = 2,
Option_C = 4,
Option_D = 8,
// blah blah
}
However, if you need an arbitrary number of options, it's probably time to change strategies. You could make a custom type that behaves like an enum, but you'll probably be better off with just a regular, non-flags enum, and a HashSet<Option>.
public enum Option
{
Option_A = 1, // notice the sequential values now
Option_B = 2,
Option_C = 3,
Option_D = 4,
}
HashSet<Option> options = new HashSet<Option> { Option.Option_A, Option.Option_C };
if(options.Contains(Option.Option_A)) // ...
Use bitwise AND to check if the bits in option are set in activeOption. You also need to make both parameters the same type so the operator will work (you are checking the bits in an Option bitmask anyway):
private static bool IsOption(Option activeOption, Option option)
{
return (activeOption & option) == option;
}
In addition to FlagsAttribute mentioned, in C# there is the Enum.HasFlag Method
using System;
[Flags] public enum DinnerItems {
None = 0,
Entree = 1,
Appetizer = 2,
Side = 4,
Dessert = 8,
Beverage = 16,
BarBeverage = 32
}
public class Example
{
public static void Main()
{
DinnerItems myOrder = DinnerItems.Appetizer | DinnerItems.Entree |
DinnerItems.Beverage | DinnerItems.Dessert;
DinnerItems flagValue = DinnerItems.Entree | DinnerItems.Beverage;
Console.WriteLine("{0} includes {1}: {2}",
myOrder, flagValue, myOrder.HasFlag(flagValue));
}
}
// The example displays the following output:
// Entree, Appetizer, Dessert, Beverage includes Entree, Beverage: True
Take a look at Enumerations that use Flag Attributes.
http://www.codeproject.com/Articles/37921/Enums-Flags-and-Csharp-Oh-my-bad-pun.aspx
If you are using .NET 4.0 you can use HasFlag.
static void Main(string[] args)
{
Option activeOption = (Option)5; // Means I activeted the Option_A and Option_C
if (activeOption.HasFlag(Option.Option_A)) { /*do work*/ }
if (activeOption.HasFlag(Option.Option_B)) { /*do work*/ }
if (activeOption.HasFlag(Option.Option_C)) { /*do work*/ }
if (activeOption.HasFlag(Option.Option_D)) { /*do work*/ }
}

Categories

Resources