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

dotnet interop

group:

Intermittent/Random Enumeration of VB6 Collection Class


Intermittent/Random Enumeration of VB6 Collection Class PatrickHRAS
4/27/2007 8:44:05 AM
dotnet interop:
I have a VB6 collection class that does not always return the correct number
of items when enumerated using a c# front end. However the enumeration only
fails in a release version of the exe. If the project is debugged in Visual
Studio or a debug version of the exe is made the enumeration always works.

The collection class’s structure is

Private m_colItem As Collection

Public Property Get Items() As Collection
Set Items = m_colItem
End Property

Public Property Get NewEnum() As IUnknown
Set NewEnum = m_colItem.[_NewEnum]
End Property

Public Function Load(Optional vWhere, Optional vOrderBy, Optional
bLoadChildren As Boolean) As Boolean
Dim rs As Recordset
Dim oCalculationRule As CUCalculationRule
Dim oDB As CDDataServices

Set oDB = CreateObject("dl.CDDataServices")

'Get all matching records.
If oDB.SelectAll(rs, "CalculationRule", vWhere, vOrderBy) Then

Set m_colItem = New Collection

'Work through the recordset.
While Not rs.EOF

'Create a new object and fill it from the recordset.
Set oCalculationRule = New CUCalculationRule
Call oCalculationRule.PopulateFromRecordset(rs)

'Add the object and add it to the collection.
m_colItem.Add oCalculationRule, CStr(oCalculationRule.IUUserService_ID)

rs.MoveNext
Wend


Load = True

End If

End Function

Private Sub Class_Initialize()

Set m_colItem = New Collection

End Sub

The collection is enumerated in c# using a foreach loop

foreach(CUCalculationRule oSchemeClass in oCalculationRuleList)
{

….
}

oCalculationRuleList is the interop wrapper for the collection class.

The architecture of the application is: UI layer written in c# (.NET 1.1),
VB6 business objects layer, VB6 COM+ data access layer and SQL Server 2000
database. Client OS is Windows 2000 and the COM+ components are hosted by a
Windows Server 2003. The collection class is part of the business objects
layer.

If you have any idea what is causing this or have experienced a similar
problem please let me know.

Thanks in advance, Patrick
Re: Intermittent/Random Enumeration of VB6 Collection Class william_aarestad NO[at]SPAM hotmail.com
5/2/2007 3:38:55 PM
On Apr 27, 10:44 am, PatrickHRAS
[quoted text, click to view]

I have just finished running into the exact same problem and find that
it is not relegated to C# but also impacts VB.Net. Ultimately, I
ended up having to get the count off of the collection passed from VB6
and creating a For i=1 to IOArray.Count in order to get all of the
elements in the collection. The count property does not show up in
intellisense but you will see it in your watch window if you are
debugging.

All I can attribute the issue to is the marshalling of the collection
of objects from VB6. In my case, the objects returned may or may not
contain an array of other objects based upon the makeup of the data
retrieved. I don't know if that has something to do with it in that
the enumerator may be somehow working based upon a memory offset and
since the object definitions are different sizes it is skipping over
elements.

I took a look through the IL of both the debug and release versions of
the assembly and did not see anything that jumped out at me as being
different. I ended up just having to be happy with the fact that the
For i loop fixed the problem.
Re: Intermittent/Random Enumeration of VB6 Collection Class PatrickHRAS
5/3/2007 12:54:01 AM
Hi William,

Thanks for the response. Could you post a sample of the code that you use to
get the elements in the collection. I had thought of a similar solution but
could not work out how to return the elements from the collection.

Patrick

[quoted text, click to view]
AddThis Social Bookmark Button