How to use switch statement with Enumerations C#
- by Maximus Decimus
I want to use a switch statement in order to avoid many if's. So I did this:
public enum Protocol
{
Http,
Ftp
}
string strProtocolType = GetProtocolTypeFromDB();
switch (strProtocolType)
{
case Protocol.Http:
{
break;
}
case Protocol.Ftp:
{
break;
}
}
but I have a problem of comparing an Enum and a String. So if I added Protocol.Http.ToString() there is another error because it allows only CONSTANT evaluation. If I change it to this
switch (Enum.Parse(typeof(Protocol), strProtocolType))
It's not possible also. So, it's possible to use in my case a switch statement or not?