【问题标题】:C# enum usage in string字符串中的 C# 枚举用法
【发布时间】:2017-07-14 11:31:43
【问题描述】:

我是 C# 新手,但尝试使用枚举。枚举如下:

public enum PARAM : int { ROLL = 1, PITCH, YAW, MAGX, MAGY, MAGZ, BCA, OAT, IAS, TAS, VOLTS, AOA };

我是这样使用的:

AhrsCom.setCommand("$out=" + PARAM.PITCH + ",10\n\r");

但是,它将以下字符串传递给setCommand(string command)

"$out=PITCH,10\n\r"

而不是out=2,10\n\r,就像我想的那样。

【问题讨论】:

  • 如果您想将枚举用作int,请将其转换为int
  • 使用 AhrsCom.setCommand("$out=" + (int)PARAM.PITCH + ",10\n\r");

标签: c# enums


【解决方案1】:

你需要

AhrsCom.setCommand("$out=" + (int)PARAM.PITCH + ",10\n\r");

说你想要数字,而不是字符串

【讨论】:

    【解决方案2】:

    您可以将枚举转换为int 以获取它的数值:

    AhrsCom.setCommand("$out=" + (int)PARAM.PITCH + ",10\n\r");
    

    【讨论】:

      【解决方案3】:

      你可以这样投射:

      AhrsCom.setCommand("$out=" + (int)PARAM.PITCH + ",10\n\r");
      

      【讨论】:

        猜你喜欢
        • 2010-10-18
        • 1970-01-01
        • 2011-11-02
        • 2014-02-22
        • 1970-01-01
        • 1970-01-01
        • 2011-02-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多