Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variant in C# ?

Status
Not open for further replies.

d00ape

Programmer
Apr 2, 2003
171
SE
I’m coding in C# and I am working with a COM object that I earlier in C++ made this call:

COleVariant varBeforeEnd(DISP_E_MEMBERNOTFOUND, VT_ERROR);
hresult = pTemp->Add(knownObject, &varBeforeEnd );

How do I make the call in C#?

The IDE in VisualStudio say that the Add(…) function should have parameters like this:

Add(KownObject, ref object);

Any tip what to do with the second parameter? I’ve tried to block some primitive types like int and bool to an object, but I only get exceptions…

All tips are welcomed!
 
first guess, create generic type Nullable<type> and cast that to object.

Code:
private Function()
{
 Add(KownObject, (object)(new Nullable<object>(obj)));
}

mr s. <;)

 
hmmmm... never usen the Nullable type.

the compiler don't like casting the:

ref object

with:

(object)(new Nullable<object>(obj))

 
perhaps that should be

Code:
private Function()
{
 Add(KownObject, ref (object)(new Nullable<object>(obj)));
}

what error are you getting?


mr s. <;)

 

object obj = new object();
temp.Add(known, ref (object)(new Nullable<object>(obj)));



----------------------------------------------------
Error 1 The type 'object' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable<T>'
----------------------------------------------------

and

----------------------------------------------------
Error 2 Cannot convert type 'object?' to 'object'
----------------------------------------------------
 
error 1 suggests you're using a reference type anyway.

you would only need to use Nullable for value types (int, double, etc).


mr s. <;)

 
Got the solution!!

Object obj = System.Reflection.Missing.Value;
temp.Add(known, ref obj);

...easy as that!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top