Thursday, December 13, 2007

COM Interop with .NET 2.0

For the month of December I am working on this small project (3-weeks) involving making some information available to end users through a mobile device. Yeah, that's cool. However, the code that grabs the information from the backend was all written in good old VB6 and classic ASP.

I am running into this one issue that is driving me nuts. I am trying to assign a class object to a property in the COM object. While it works for some properties/methods, it does not seem to be working for some other.


VB6 code that works:
[
    1 Private mAuthor As clsSomething
    2 Private mAuthorID As Long
    3 
    4 Public Property Get Author_ClassParam() As clsSomething
    5     On Error GoTo handleError
    6 
    7     If mAuthor Is Nothing Then
    8         If mObjectID = 0 Then
    9             Err.Raise 111, , "xxx item not initialized"
   10         End If
   11 
   12         Set mAuthor = New clsSomething
   13         [--- deleted stuff---]
   14 
   15     End If
   16     Set Author = mAuthor
   17     Exit Property
   18 
   19 handleError:
   20     mError.Raise Err.number, "[xxxxHistoryItem]" & Err.Description, False
   21     Set Author = Nothing
   22 End Property
   23 
   24 Public Property Set Author_ClassParam(ByVal value As clsSomething)
   25     Set mAuthor = value
   26 End Property
]

VB6 code that does not works:
[
    1 Private mAuthor As clsSomething
    2 Private mAuthorID As Long
    3 
    4 Public Property Get Author_VariantParam() As clsSomething
    5     On Error GoTo handleError
    6 
    7     If mAuthor Is Nothing Then
    8         If mObjectID = 0 Then
    9             Err.Raise 111, , "xxx item not initialized"
   10         End If
   11 
   12         Set mAuthor = New clsSomething
   13         [--- deleted stuff---]
   14 
   15     End If
   16     Set Author = mAuthor
   17     Exit Property
   18 
   19 handleError:
   20     mError.Raise Err.number, "[xxxxHistoryItem]" & Err.Description, False
   21     Set Author = Nothing
   22 End Property
   23 
   24 Public Property Set Author_VariantParam(ByVal value As Variant)
   25     Set mAuthor = value
   26 End Property
]


this is the .NET code that calls that VB6 COM object:
[
    1 public void ClassAssignment_COM()
    2 {
    3     clsSomething oAuthor = new clsSomething();
    4     oAuthor.AuthorID = 15;
    5 
    6     clsTransaction oTrnx = new clsTransaction();
    7     oTrnx.Author_ClassParam = oAuthor;
    8     Trace.WriteLine(String.Format("bts- ClassParam.AuthorID: [{0}]", oTrnx.Author_ClassParam.AuthorID));
    9 }
   10 
   11 public void VariantAssignment_COM()
   12 {
   13     clsSomething oAuthor = new clsSomething();
   14     oAuthor.AuthorID = 15;
   15 
   16     clsTransaction oTrnx = new clsTransaction();
   17     oTrnx.Author_VariantParam = oAuthor;
   18     Trace.WriteLine(String.Format("bts- VariantParam.AuthorID: [{0}]", oTrnx.Author_VariantParam.AuthorID));
   19 }
]

I get this error message at compile time:
Error 3 Property, indexer, or event 'Author_VariantParam' is not supported by the language; try directly calling accessor methods 'xxxx._clsTransaction.get_Author_VariantParam' or 'xxxx._clsTransaction.set_Author_VariantParam(object)'
C:\vsProjects\Tests\mock.cs 168 27 xxxx.yyyApplication.Web.Test


I have not found a solution to this problem. I know that variants are not supported in .NET, but com'on, there's got to be someone out there that knows a workaround this issue...