views:

40

answers:

1

I'm working on a VB6 application that consumes .NET objects via COM Interop. The application is working well, but I feel like I'm taking a performance hit each time I instantiate a .NET object from within VB6.

For example, I have VB6 code that loops through a recordset and instantiates new .NET objects for each item in the recordset and adds it to an array (CartItem and DiscountEngine are both .NET objects):

  Set lCartItemClass = New CartItem
  Set lCartItem = lCartItemClass
  lCartItem.SKU = .Fields("SKU").Value
  lCartItem.Quantity = .Fields("Quantity").Value
  Set lCartItemsClass(i) = lCartItem

  '...   '

  mCartClass.CartItems = lCartItemsClass
  Set mDiscountEngine.Cart = mCartClass

Would I find a performance benefit by adding a factory method that takes in the parameters I want for my properties and handles the object instantiation on the .NET side of the execution? So instead of the above code, I would have something like:

  mDiscountEngine.Cart.AddCartItem( .Fields("SKU").Value, _
      .Fields("Quantity").Value)
A: 

Making calls across interop is always going to be expensive. I don't think the solution that you are describing will improve performance. Best bet would to loop through the recordset in vb6 adding each property to an array e.g. ary_SKU(), ary_Quantity(), etc. Then once finished pass the arrays as parameters to .Net and loop through the arrays again on the .net side and build up the classes from the data in the arrays

Andrew