views:

115

answers:

1

We are working on exposing an assembly to COM.

Among other things, we frequency use nullable values such as long?, DateTime?, etc. These are generic types and can't be exposed to COM.

What is a good substitute for these data types for COM?

We have tried the following:

//Original CustomerID property in class
public long? CustomerID
{
   get;
   set;
}

//Explicit COM interface
long IComInterface.CustomerID
{
  get { return CustomerID.GetValueOrDefault(); }
  set { CustomerID = value; }
}

The problem is, we need a way to pass "null" back and forth through COM. Using a number like -1 or 0 won't work because these are valid values as well.

We are having to use nullables b/c these originally came from our database schema.

+3  A: 

What about using a Variant (VT_DATE) on the COM side? If you declare the member as object, you should be able to pass a DateTime or null and COM interop should handle it ok. See the following on MSDN for more details.

Marshaling Object to Variant

Josh Einstein
Does this work for long?, float?, double? we use many different nullable types.
Jonathan.Peppers
Yes, those types can all be represented as a variant. The link at the bottom of my answer should take you right to the table that shows what variant types the primitive types map to as variants.
Josh Einstein
Agree. I don't know what's on the other side (the client) of your COM interface, but this is the best option. For many environments anything else will be at best contrived, at worst not work at all (e.g. VBScript).
Euro Micelli
One quick question: in their example they use an interface MarshalObject that doesn't appear to be in the framework. So this looks to me like I need to just create a more target-purpose interface that wraps any nullable value, it could have methods for HasValue, ToInt(), ToLong(), etc.
Jonathan.Peppers
I think that interface was just used to demonstrate how a managed interface with various signatures of different types is seen by COM. From what I gathered reading the documentation, Object is marshaled as a VARIANT as long as the signature is of type System.Object.
Josh Einstein
I understand. I should just mark these types as object and they should come out as VARIANT on the COM client side.
Jonathan.Peppers