问题描述
public interface IStatusMonitor
{
[OperationContract]
int MonitorStates(MonitorableItemFlags aFlags);
}
[DataContract, Flags]
public enum MonitorableItemFlags : uint
{
[EnumMember] None = 0x00000000,
[EnumMember] SystemState = 0x00000001,
[EnumMember] PatientDetectState = 0x00000002,
[EnumMember] SpecialtyPalletState = 0x00000004,
[EnumMember] AxisHomedState = 0x00000008,
[EnumMember] PowerState = 0x00000010,
[EnumMember] HandControllerButtonState = 0x00000020,
[EnumMember] MotionInhibitState = 0x00000040,
[EnumMember] BedConfiguration = 0x00010000,
[EnumMember] DetectorConfiguration = 0x00020000,
[EnumMember] DeviceConfiguration = 0x00040000,
[EnumMember] BedPivotRange = 0x00080000
}
这是实现 public int MonitorStates(MonitorableItemFlags aFlags) { 如果(aFlags.HasFlag(MonitorableItemFlags.SystemState)) { 返回25; } 如果(aFlags.HasFlag(MonitorableItemFlags.PatientDetectState)) { 返回5; } 如果(aFlags.HasFlag(MonitorableItemFlags.PowerState)) { 返回6; } 如果(aFlags.HasFlag(MonitorableItemFlags.AxisHomedState)) { 返回9; } 如果(aFlags.HasFlag(MonitorableItemFlags.SpecialtyPalletState)) { 返回15; 如果(aFlags.HasFlag(MonitorableItemFlags.HandControllerButtonState)) { //未实现 }
This is implementation public int MonitorStates(MonitorableItemFlags aFlags) { if (aFlags.HasFlag(MonitorableItemFlags.SystemState)) { return 25; } if (aFlags.HasFlag(MonitorableItemFlags.PatientDetectState)) { return 5; } if (aFlags.HasFlag(MonitorableItemFlags.PowerState)) { return 6; } if (aFlags.HasFlag(MonitorableItemFlags.AxisHomedState)) { return 9; } if (aFlags.HasFlag(MonitorableItemFlags.SpecialtyPalletState)) { return 15; if (aFlags.HasFlag(MonitorableItemFlags.HandControllerButtonState)) { //not implememnted }
&private UInt32 myMoniterFlags;
private UInt32 myMoniterFlags;
调用statusMoniterWrapper.MonitorStates(moniterFlags);
while calling statusMoniterWrapper.MonitorStates(moniterFlags);
此处的myMoniterFlags标志是所选枚举的总和.
我的问题是有什么方法可以删除if条件.
here myMoniterFlags flags is sum of the selected enums.
My question is is there any way to remove the if condition.
任何形式的代码都非常适用.
Any peice of code is much appericiated.
推荐答案
在您的代码中,您正在检查MoniterFlags标志值,该条件满足该条件.在您的代码中,您需要if条件来验证它属于哪个枚举.您可以使用的另一种方法是单行"if"
In your code you are checking MoniterFlags flags value in which condition it satisfies. In your code you need if condition to validate in which enum it falls. Another way you could use is single line "if"
条件? first_expression:second_expression;
condition ? first_expression : second_expression;
您还可以详细说明为什么要删除如果有条件"吗?
Also could you please elaborate why you want to remove "if condition"?
这篇关于从枚举标志中删除条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!