简体   繁体   中英

(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!

Just cast the enum as an int like

DBSelect((int)DBEnum.DB1);

See this previous question about casting enum to ints. Get int value from enum

Edit: Also you should consider keeping your connection strings in the configuration manager. See this MSDN article on how to do that

https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.connectionstrings(v=vs.110).aspx

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)

It asks if you're "missing a cast" because enum s and int s are different data types. Like the others said, you need to type-cast it:

DBSelect((int)DBEnum.DB1);

Otherwise you'll keep getting the "Cannot explicitly convert enum to int32 "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM