Enum error while doing check
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)
{
S开发者_JS百科ubItems.Add(Data.value)
}
But i am getting an error "Cannot implicitly convert type 'ConnectionState' to 'bool'".kindly suggest me how to proceed with this
Should it not be something like
if(YourObject.ConnectionState == ConnectionState.Connected)
{
SubItems.Add(Data.value)
}
ConnectionState
in this example is a type definition, you need to instantiate a ConnectionState object:
ConnectionState myState = new ConnectionState();
Then you can set your state:
myState = ConnectionState.Connected;
And check it with:
if(myState == ConnectionState.Connected)
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.
精彩评论