Late Binding / CF

J

James Hetfield

Hello

I am coding a DLL which should run in Full and Compact Framework. Everything
is working ok, but I wanted to use this:

Dim dr As Object
If CurrentView = ViewMode.ServiceManager Then
dr = New SqlDataAdapter(SQL, ServerConnection)
Else
dr = New SqlCEDataAdapter(SQL, MobileConnection)
End if

But I get the error "Late Binding not supported"

Is there an easy way to get around this problem?

THanks

James
 
C

Christopher Fairbairn [MVP]

Hi James.

James Hetfield said:
But I get the error "Late Binding not supported"

Is there an easy way to get around this problem?

I assume there is some additional code after the snippet you provided, such
as something like the following:

Dim count as Int16 = dr.FieldCount

It will be this code which is causing the late binding error during
compilation.

VB.NET supports a feature called Late Binding. The 'dr' variable is typed as
Object, yet on the desktop you can call methods and properties on it, as if
it was typed as SqlCEDataAdapter or SqlDataAdapter.

It is only when the application runs (i.e. "later" than compilation where
this check is usually made) that the runtime determines if these methods
actually exist and "binds" to them or throws an exception.

At compile time the compiler only knows it has a variable of type Object, so
it can not determine if the method or function calls you make in this manor
are valid.

Although late binding works on the desktop, as discussed in the article
"Differences from Desktop in .NET Compact Framework Development" (available
at http://msdn.microsoft.com/en-us/library/t340010s.aspx) it is not a
supported feature when targeting a PDA.

As such replacing the line

Dim dr as Object

with

Dim dr as IDbDataAdapter

Should allow you to work with either data adapter. This doesn't require late
binding as both classes implement the IDbDataAdapter interface and hence can
be guarenteed at compile time to contain the methods and properties it
defines.

Hope this helps,
Christopher Fairbairn
 
J

James Hetfield

Thanks

Christopher Fairbairn said:
Hi James.



I assume there is some additional code after the snippet you provided,
such as something like the following:

Dim count as Int16 = dr.FieldCount

It will be this code which is causing the late binding error during
compilation.

VB.NET supports a feature called Late Binding. The 'dr' variable is typed
as Object, yet on the desktop you can call methods and properties on it,
as if it was typed as SqlCEDataAdapter or SqlDataAdapter.

It is only when the application runs (i.e. "later" than compilation where
this check is usually made) that the runtime determines if these methods
actually exist and "binds" to them or throws an exception.

At compile time the compiler only knows it has a variable of type Object,
so it can not determine if the method or function calls you make in this
manor are valid.

Although late binding works on the desktop, as discussed in the article
"Differences from Desktop in .NET Compact Framework Development"
(available at http://msdn.microsoft.com/en-us/library/t340010s.aspx) it is
not a supported feature when targeting a PDA.

As such replacing the line

Dim dr as Object

with

Dim dr as IDbDataAdapter

Should allow you to work with either data adapter. This doesn't require
late binding as both classes implement the IDbDataAdapter interface and
hence can be guarenteed at compile time to contain the methods and
properties it defines.

Hope this helps,
Christopher Fairbairn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top