C#字符串中的枚举用法

C#字符串中的枚举用法,c#,enums,C#,Enums,我对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"); 我使用它的方式如下: public enum PARAM : int { ROLL = 1, PITCH, YAW, MAGX, MAGY,

我对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");
我使用它的方式如下:

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(字符串命令)

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

您需要

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

要说您想要的是数字而不是字符串,您可以将枚举强制转换为
int
,以获取其数值:

AhrsCom.setCommand("$out=" + (int)PARAM.PITCH + ",10\n\r");
你可以这样投:

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

如果要将枚举用作
int
,请将其强制转换为
int
使用AhrsCom.setCommand(“$out=“+(int)PARAM.PITCH+”,10\n\r”);可能重复的