Search code examples
c#enumscastingintenumerator

(C#) Enum won't return int


I am writing an application that needs to query multiple databases, so to standardize my connection strings, I wrote the following enum and method:

class Program
{
    enum DBEnum { DB1, DB2, DB3, DB4, DB5 }

    static void Main(string[] args)
    {
        using (CacheConnection myConnection = new CacheConnection())
        {
            myConnection.ConnectionTimeout = 9999;
            myConnection.ConnectionString = DBSelect(DBEnum.DB1);
            myConnection.Open();
        }

    }

    public static string DBSelect(int i)
    {
        string connectionString = "";

        switch (i)
        {
            case 0:
                connectionString = *connection string*;
                break;
            case 1:
                connectionString = *connection string*;
                break;
            case 2:
                connectionString = *connection string*;
                break;
            case 3:
                connectionString = *connection string*;
                break;
            case 4:
                connectionString = *connection string*;
                break;
            default:
                break;
        }
        return connectionString;
    }

}

But the problem is that it isn't assigning a numeric value to the enum definitions.

According to MSDN, unless the enum is casted to a different data type, or the definitions are specifically defined, the definitions should have an int value starting with 0.

However intellisense gripes to me that the line:

 myConnection.ConnectionString = DBSelect(DBEnum.DB1); 

has invalid arguments, and if I say something like

int i = DBEnum.DB1;

it asks me if I'm missing a cast.

Thanks!


Solution

  • It is assigning a numeric value, that's not the problem. You are trying to use it as an int and not an enum. There's no implicit conversion from your enum to int. There is an explicit however. Just "cast" it

    DBSelect((int)DBEnum.DB1);
    

    but even better would be to change the method signature and implementation

    public static string DBSelect(DBEnume i)
    {
        string connectionString = "";
    
        switch (i)
        {
            case DB1:
                connectionString = *connection string*;
                break;
            case DB2:
                connectionString = *connection string*;
                break;
            case DB3:
                connectionString = *connection string*;
                break;
            case DB4:
                connectionString = *connection string*;
                break;
            case DB5:
                connectionString = *connection string*;
                break;
            default:
                throw new InvalidOperation();
                break;
        }
        return connectionString;
    }
    

    and then you should give the enum cases better names. Is not constraining you to use only the declared values. Any value of the base type can be cast into an enum. So in your case (DBEnum)10012 is valid. However enum works really well when instead of a simple sequence as you have you provide information

    DBEnum {
       UserDb,
       ArticleDb,
       RenderedCacheDb,
    }
    

    and so forth (Of course I don't know the names of your DBs so I picked random ones)