all groups > dotnet interop > april 2004 >
You're in the

dotnet interop

group:

object to variant in parameter: ref object o



object to variant in parameter: ref object o Morten Lyhr
4/21/2004 2:41:04 AM
dotnet interop: He

I have made a interop assemby with VS 2003 on a COM dll

I have the following problem

A Sub in the COM dll has the following signature
Sub Assign(ByVal FieldData As Variant

VS generates the following method
public void Assign(ref object FieldData

The variant can be several different types, Boolean, Integer, String etc

The COM also have a get method, where the return type is a variant. In .NET the return type is object, and I am able to type cast it to (bool, int, string etc.) when I call the get method. If I use the object just return from get in assign, I get a type error from the COM dll

How do I pass the object to the COM Assign Sub, So that it is in the correct type. (Boolean or another type?

RE: object to variant in parameter: ref object o Morten Lyhr
4/21/2004 11:41:04 PM
// Simplified version

//Creates a new instance of the CO
COMClass comClass = new COMClass()

//Opens a storag
comClass.Open(@"C:\file.bin")

//Gets a variant(in .NET -> object) from the storag
//It can be of several different types (bool, string, int etc.
object o = comClass.Get(1)
//If I know it is a bool(there is a GetType Method
bool b = (bool)o; /*WORKS!*
b = false; /* WORKS*

//Assign a variant back to the storage
//Fails, even if it is the value from Get and it has not been modifed
AddThis Social Bookmark Button