ekekakos
ekekakos

Reputation: 611

Transfer a CASE from VB to SWITCH in C#

I have the following code in VB.

Public Shared Function CreateConnection() As IDbConnection
  Select Case UCase(DatabaseProvider)
        Case "DEVARTUNIVERSAL"
            Dim connection As New UniConnection

            Return connection
        Case "ACCESS"
            Dim connection As New OleDbConnection

            Return connection
        Case "MSQLSERVER2005"
            Dim connection As New SqlConnection

            Return connection
        Case Else
           Throw New Exception("Please select a correct database provider")

        Return Nothing
  End Select

End Function

I put it in a utility and create the following code in c#

public static IDbConnection CreateConnection()
  {
  switch ((DatabaseProvider.ToUpper()))
     {
     case "DEVARTUNIVERSAL":
        UniConnection connection = new UniConnection();

        return connection;                
     case "ACCESS":
        OleDbConnection connection = new OleDbConnection();

        return connection;
     case "MSQLSERVER2005":
        SqlConnection connection = new SqlConnection();

        return connection;
     default:
        throw new Exception("Please select a correct database provider");

        return null;
     }

  }

Is it correct? According to VS2015 all variables CONNECTION after the 1st case are in error with the message "A local variable 'connection' is already defined in this scope".

Can someone help me on this? Thanks

Upvotes: 2

Views: 115

Answers (4)

Magnus
Magnus

Reputation: 46947

Just wrap your case in a scope {}:

case "DEVARTUNIVERSAL":
{
   UniConnection connection = new UniConnection();
   return connection;  
}

Or if there is no other code in the case, return the object directly.

case "DEVARTUNIVERSAL":
   return new UniConnection();

Upvotes: 2

Armand P.
Armand P.

Reputation: 41

It is almost correct. The point is that you name many variable "connection". Moreover, it looks like you don't need variables here : you could do :

return new UniConnection();

Otherwise, you can declare your connection variable above the switch :

IDbConnection connection;

and then use it in the switch :

connection = new UniConnection();

In C# the scope is only determined by braces, and that is why, in this case, theses variable declarations are considered in the same scope.

I hope my answer will help you !

Upvotes: 0

AD-
AD-

Reputation: 61

you can't use the same variable name for that connection. try this :

public static IDbConnection CreateConnection()
{
 switch ((DatabaseProvider.ToUpper()))
 {
 case "DEVARTUNIVERSAL":
    UniConnection uniConnection = new UniConnection();

    return uniConnection; 

 case "ACCESS":
    OleDbConnection oleDbConnection = new OleDbConnection();

    return oleDbConnection;

 case "MSQLSERVER2005":
    SqlConnection sqlConnection = new SqlConnection();

    return sqlConnection;

 default:
    throw new Exception("Please select a correct database provider");

    return null;
 }

 }

Upvotes: 0

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174515

Either declare the variable outside the switch:

public static IDbConnection CreateConnection()
{
    IDbConnection connection;
    switch ((DatabaseProvider.ToUpper()))
    {
        case "DEVARTUNIVERSAL":
            connection = new UniConnection();
            return connection;                
        case "ACCESS":
            connection = new OleDbConnection();
            return connection;
        case "MSQLSERVER2005":
            connection = new SqlConnection();
            return connection;
        default:
            throw new Exception("Please select a correct database provider");

    }
    return null;
}

Or skip the variable assignment altogether:

public static IDbConnection CreateConnection()
{
    switch ((DatabaseProvider.ToUpper()))
    {
        case "DEVARTUNIVERSAL":
            return new UniConnection();
        case "ACCESS":
            return new OleDbConnection();
        case "MSQLSERVER2005":
            return new SqlConnection();
        default:
            throw new Exception("Please select a correct database provider");
    }
    return null;
}

Upvotes: 2

Related Questions