bungpeng Posted October 26, 2006 Posted October 26, 2006 This code work fine in 1.1 but not 2.0. Any help? Thank you. 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) Quote
Administrators PlausiblyDamp Posted October 26, 2006 Administrators Posted October 26, 2006 As a quickie - you could declare Con as System.Data.IDbConnection rather than object, at least then you will get intellisense to help you. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
bungpeng Posted October 26, 2006 Author Posted October 26, 2006 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? Quote
Administrators PlausiblyDamp Posted October 26, 2006 Administrators Posted October 26, 2006 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? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
bungpeng Posted October 26, 2006 Author Posted October 26, 2006 No, I mean what if I create 2 similar classes and object refer to one of it dynamically? Quote
Administrators PlausiblyDamp Posted October 26, 2006 Administrators Posted October 26, 2006 Define a common interface that both classes implement and declare your variable as that interface. 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 Dim ex As IExample If True Then ex = New ExampleOne Else ex = New ExampleTwo End If ex.ExampleSub() Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.