1

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!

Brian B.
  • 89
  • 11
  • 3
    Why don't you change the method to take a `DBEnum` instead of an `int`? – juharr Nov 29 '16 at 20:43
  • 1
    @juharr because I'm a noob. Thank you for that suggestion! – Brian B. Nov 29 '16 at 20:52
  • Possible duplicate of [Why enums require an explicit cast to int type?](http://stackoverflow.com/questions/4728295/why-enums-require-an-explicit-cast-to-int-type) – Mong Zhu Nov 29 '16 at 20:53

3 Answers3

3

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

Community
  • 1
  • 1
Sam Marion
  • 690
  • 1
  • 4
  • 17
3

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)

Rune FS
  • 21,497
  • 7
  • 62
  • 96
  • That makes alot of sense. Thank you very much. – Brian B. Nov 29 '16 at 20:49
  • Also, I have more detailed names, but just for the sake of an example, and for security reasons, I just changed it to DB1. BUT i did change the method to take the enum itself, rather than an int. Thank you very much. – Brian B. Nov 29 '16 at 20:57
0

It asks if you're "missing a cast" because enums and ints 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"

Uchiha Itachi
  • 1,251
  • 1
  • 16
  • 42