如何在IF循环中设置枚举

时间:2011-02-03 10:50:39

标签: c# .net

我有一个名为“stateType”的枚举。

  enum stateType : int
  {
     Unknown = 0,
     Active = 1,
     Inactive = 2
  } 

在我的功能“connection()”中显示如下,我只需要在时间显示版本 枚举“活跃”。

static private void connction()
{
    string hostName = this_event.variableData[0].atr_value;
    string policyGuid = this_event.variableData[1].atr_value;
    string policyVersion = this_event.variableData[2].atr_value;
    string formatVersion = this_event.variableData[3].atr_value;
    string enabled = this_event.variableData[4].atr_value;
    string Version = "0.0.0.0";
    if (this_event.variableData.Length >= 6)
    {
        Version = this_event.variableData[5].atr_value;
    }
}

我将如何做到这一点,我需要在

中设置一个条件
if loop(if (this_event.variableData.Length >= 6)&& condition )

我这样做了

         if (this_event.variableData.Length >= 6 && stateType.Active)
                                {
                                    Version = this_event.variableData[5].atr_value;
                                }

我收到错误操作员'&&'不能应用于'bool'和'Spo.SPDlib.SPD.SPD_clientStateType'的操作数'D:\ P \ leaf.cs

5 个答案:

答案 0 :(得分:1)

您收到错误是因为您从未将状态变量分配给任何内容。

如果我理解你,你想要这样的东西:

if (this_event.variableData.Length >= 6 && stateType.Active == SPD.SPD_clientStateType.SPD_clientActive)
{
    Version = this_event.variableData[5].atr_value;
}

答案 1 :(得分:1)

您需要根据所提取的数据了解StateType的内容,只有在此之后您才能比较它。

您的对象模型应该指出其ActiveInactive

答案 2 :(得分:0)

statenull。当您声明stateType state时,它的值为null。由于它是enum,您可以像stateType.Active一样使用它,而不是声明一个新变量。

答案 3 :(得分:0)

您需要为州

定义一些值
stateType state = something;

答案 4 :(得分:0)

stateType state = stateType.Active;这是正确的。