检查时枚举错误
我有一个像这样的枚举,
public enum ConnectionState : int
{
Unknown = 1,
Connected = 2,
Disconnected = 3,
}
如果已连接,我需要显示值,我做了此检查
if(ConnectionState.Connected)
{
SubItems.Add(Data.value)
}
,但我收到错误“无法将类型'ConnectionState'隐式转换为'bool'”。请建议我如何继续进行此操作
I have an enum like this
public enum ConnectionState : int
{
Unknown = 1,
Connected = 2,
Disconnected = 3,
}
I need to display value if it is connected i did this check
if(ConnectionState.Connected)
{
SubItems.Add(Data.value)
}
But i am getting an error "Cannot implicitly convert type 'ConnectionState' to 'bool'".kindly suggest me how to proceed with this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不应该是这样的吗
Should it not be something like
本例中的
ConnectionState
是一个类型定义,您需要实例化一个 ConnectionState 对象:然后您可以设置您的状态:
并使用以下命令检查它:
ConnectionState
in this example is a type definition, you need to instantiate a ConnectionState object:Then you can set your state:
And check it with:
枚举或枚举数据类型,顾名思义是用户定义的数据类型。因此它们不能直接在条件语句中使用。它们可以在自身内部进行比较。因此您需要定义对象,然后将其值与中的其他值进行比较枚举。
Enums or enumerated data types ,as the name suggests are user defined datatypes.So they can not used directly in the condition statements.They can be compared within themselves.So you need to define your object and then compare its value with some other value in enum.