我有以下案例:
enum values{
go = 1,
stand = 2,
jump = 3,
run = 4,
go_stand=5,
go_jump=6,
go_run=7,
go_stand_jump=8,
… and so on
Go_stand_jum_prun=17}
int select value(bool go, bool stand, bool jump, bool run)
{
}
基于传递给方法的bool值的组合,我需要返回适当的值,这意味着如果go = true,如果传入的所有参数都为真,则返回false false 1将返回,同样适用于所有两者之间的组合。唯一的想法我有很多if和else if语句来评估可能的组合,这有点难看。问题是有更优雅的解决方案。
谢谢大家!!!
答案 0 :(得分:7)
要实现此目的,您必须修改enum
enum values {
none = 0,
go = ( 1 << 0 ), // 1
stand = ( 1 << 1 ), // 2
jump = ( 1 << 2 ), // 4,
run = ( 1 << 3 ), // 8
go_jump = go | jump
}
然后在你的方法中:
values GetValues(bool go, bool stand, bool jump, bool run)
{
values result = values.none;
if( go )
result |= values.go;
if( stand )
result |= values.stand;
// and so on...
return result;
}
答案 1 :(得分:5)
您可以使用flags enum将值合并为单个枚举值。
表示可以将枚举视为位字段;也就是说,一组标志。
这是一个例子: (Dot Net Fiddle)
using System;
[Flags]
public enum Value
{
None = 0,
Go = 1,
Stand = 2,
Jump = 4,
Run = 8
}
public class Program
{
public static void Main()
{
var value1 = CreateValue(true, false, true, false);
var value2 = CreateValue(false, true, true, true);
Console.WriteLine(value1); //Go, Jump
Console.WriteLine(value2); //Stand, Jump, Run
}
public static Value CreateValue(bool go, bool stand, bool jump, bool run)
{
var value = Value.None;
if(go)
value |= Value.Go;
if(stand)
value |= Value.Stand;
if(jump)
value |= Value.Jump;
if(run)
value |= Value.Run;
return value;
}
}
答案 2 :(得分:3)
将FlagsAttribute应用于您的枚举:
[Flags]
public enum Activity
{
None = 0,
Go = 1,
Stand = 2,
Jump = 4,
Run = 8,
}
并使用conditional operator构建枚举:
public static Activity CreateActivity(bool go, bool stand, bool jump, bool run)
{
return
(go ? Activity.Go : 0) |
(stand ? Activity.Stand : 0) |
(jump ? Activity.Jump : 0) |
(run ? Activity.Run : 0);
}
完整示例:
using System;
public class Program
{
public static void Main()
{
Activity goJump = Activity.Go | Activity.Jump;
Activity activity =
CreateActivity(
go: true,
stand: false,
jump: true,
run: false);
Console.WriteLine(activity == goJump); // prints "True"
}
public static Activity CreateActivity(bool go, bool stand, bool jump, bool run)
{
return
(go ? Activity.Go : 0) |
(stand ? Activity.Stand : 0) |
(jump ? Activity.Jump : 0) |
(run ? Activity.Run : 0);
}
}
[Flags]
public enum Activity
{
None = 0,
Go = 1,
Stand = 2,
Jump = 4,
Run = 8,
}
答案 3 :(得分:2)
您可以使用逐位数学。例如,这是你的班级:
class MyClass
{
[Flags]
public enum Values
{
Go = 1,
Stand = 2,
Jump = 4,
Run = 8
}
public Values Select(bool go, bool stand, bool jump, bool run)
{
Values ret = 0;
if (go) ret = (ret | Values.Go);
if (stand) ret = (ret | Values.Stand);
if (jump) ret = (ret | Values.Jump);
if (run) ret = (ret | Values.Run);
return ret;
}
}
以下是你如何使用它:
var mc = new MyClass();
MyClass.Values y = mc.Select(true, false, true, true);
bool isStand = (y & MyClass.Values.Stand) == MyClass.Values.Stand; // false
bool isJump = (y & MyClass.Values.Jump) == MyClass.Values.Jump; // true
答案 4 :(得分:0)
如果您排列未正确显示的值(如果没有正确显示)并以0(而不是1)开头,则此伪代码应该可以使用。
int select value(bool go, bool stand, bool jump, bool run)
{
return (int) runx2^3 + (int)jumpx2^2 + (int) standx2^1 + (int) go;
}
答案 5 :(得分:-1)
public enum MyEnum
{
Nothing = 0,
Run = 1,
Jump = 2,
Stand = 4,
Go = 8,
RunJump = Run + Jump,
RunStand = Run + Stand,
...
}
public MyEnum GetValue(bool go, bool stand, bool jump, bool run)
{
// true, true, true, true is 1111
// true, true, true, false is 1110
var returnValue = (go ? 1 << 3 : 0) + (stand ? 1 << 2 : 0) + (jump ? 1 << 1 : 0) + (run ? 1 : 0);
return (MyEnum)returnValue;
}