How to use switch statement with Enumerations C#

Posted by Maximus Decimus on Stack Overflow See other posts from Stack Overflow or by Maximus Decimus
Published on 2013-11-13T15:43:29Z Indexed on 2013/11/13 15:53 UTC
Read the original article Hit count: 174

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about enums