tags:

views:

309

answers:

1

In C# I'm calling a function on an interop class (Connection, written in VB6), which itself returns a plain object (System). Then there's a function on that object (InitialiseData) that I also call (even though the compiler isn't aware of it).

var isInitialised = Connection.System.InitialiseData(path);

The problem is, because that System object is returning a mere Object, I get the compile error "cannot resolve symbol". I can see why, but the same thing works fine in VB.NET:

Dim isInitialised As Boolean = Connection.System.InitialiseData(path)

Presumably VB.NET is doing something in the background that allows a kind of implicit late-binding, whereas C# does not? Is there any way to get C# to compile in a similarly ignorant way?

In fact in this case I seem to be able to work around it by casting the Object to the explicit interop type first:

var system = (QLO.System)Connection.System;
var isInitialised = system.InitialiseData(path);

But I'm still interested in answers for cases where I won't be able to cast to a type, but still want to call functions on it. Forgive me if this is a dumb question, I'm only just starting to move from VB.NET to C#... thanks!

+1  A: 

Late binding is coming to C# in 4.0. What you can do in the mean time is wrap the function in VB.NET and use C# to call the VB.NET method which calls the VB6 method.

Since casting it to the proper type works in C#, does that mean that VB.NET sees the correct type and C# sees it as Object?

Samuel