Problem from .NET 1.1 to 2.0... (Overload resolution failed...)

bungpeng

Senior Contributor
Joined
Sep 10, 2002
Messages
906
Location
Malaysia
This code work fine in 1.1 but not 2.0. Any help? Thank you.

Code:
Dim Con As Object
        Dim sDB As String = "SQL"

        If sDB = "SQL" Then
            Con = New System.Data.SqlClient.SqlConnection
        Else
            Con = New System.Data.OleDb.OleDbConnection
        End If
        Con.Open("xxx")

I get this error:
System.MissingMemberException was unhandled by user code
Message="Overload resolution failed because no accessible 'Open' accepts this number of arguments."
Source="Microsoft.VisualBasic"
StackTrace:
at Microsoft.VisualBasic.CompilerServices.OverloadResolution.ResolveOverloadedCall(String MethodName, MemberInfo[] Members, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, BindingFlags LookupFlags, Boolean ReportErrors, ResolutionFailure& Failure)
at Microsoft.VisualBasic.CompilerServices.NewLateBinding.ResolveCall(Container BaseReference, String MethodName, MemberInfo[] Members, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, BindingFlags LookupFlags, Boolean ReportErrors, ResolutionFailure& Failure)
at Microsoft.VisualBasic.CompilerServices.NewLateBinding.CallMethod(Container BaseReference, String MethodName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, BindingFlags InvocationFlags, Boolean ReportErrors, ResolutionFailure& Failure)
at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn)
at _Default.Page_Load(Object sender, EventArgs e) in C:\Documents and Settings\bpchia\My Documents\Visual Studio 2005\WebSites\WebSite1\Default.aspx.vb:line 16
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
 
What if in other case, it does not have something like .IDbConnection object?
It is not possible to create 'object' variable and assign object to it later?
 
If you have a variable called 'Con' and the two options you present both assign a database connection to it (which means they implement IDbConnection) why would 1) I assume it was meant to hold anything other than a database connection or 2) you want to store anything other than a database connection in it?
 
Define a common interface that both classes implement and declare your variable as that interface.

Visual Basic:
Interface IExample
    Sub ExampleSub()
    Function ExampleFunction(ByVal exampleParameter As String) as String
End Interface

Class ExampleOne
    Implements IExample

    Public Function ExampleFunction(ByVal exampleParameter As String) As string Implements IExample.ExampleFunction
return string.Empty
    End Function

    Public Sub ExampleSub() Implements IExample.ExampleSub

    End Sub
End Class

Class ExampleTwo
    Implements IExample

    Public Function ExampleFunction(ByVal exampleParameter As String) As string Implements IExample.ExampleFunction
        Return String.Empty
    End Function

    Public Sub ExampleSub() Implements IExample.ExampleSub

    End Sub
End Class

You can then use this like
Visual Basic:
        Dim ex As IExample

        If True Then
            ex = New ExampleOne
        Else
            ex = New ExampleTwo
        End If

        ex.ExampleSub()
 
Back
Top