Array Results are returned as ComArray objects

Arrays require special treatment in order to be used in Visual FoxPro and for this purpose implicit method calls and property accessors use a ComArray object to pass arrays to and from FoxPro. Instead any Array return value from InvokeMethod, Set/GetProperty is returned and can be passed as an internal ComArray object that has an Instance and Count property as well as a number of array management methods to Add and Remove elements.

The returned object can then be used more easily for iterating over items or adding/editing/removing items from the array using the wwDotNetBridge methods to manipulate arrays which require a parent object instance.

ComArray Translation only works with Indirect Invokation
Automatic ComArray translation to and from .NET arrays only happens when using the indirect InvokeMethod, Get/SetProperty/ex methods of the wwDotNetBridge, since these methods hook into the call sequence for the .NET methods. If you acccess .NET COM objects directly you need to use the various native array functions provided by wwDotNetBridge.

Retrieving and Updating a .NET Array with ComArray
The following demonstrates how you can call a .NET object method with InvokeMethod() that in this case returns an array result, manipulate the array and then send it back to the server:

Do wwDotNetBridge
loBridge = CREATEOBJECT("wwDotNetBridge")

loService = loBridge.CreateInstance("WebStoreServiceProxy.InventoryService")

*** Service Method Returns an Array of Objects
loItems = loBridge.InvokeMethod(loService,"DownloadInventoryItems","")  && ComArray instance returned
FOR lnX = 0 TO loItems.Count-1   && 0 based array
  loItem = loItems.Item(lnX)  
  ? loItem.Sku + " " + loItem.Descript + " " + TRANSFORM(loItem.Price)
ENDFOR

? loItems.Count   && 48
loNewItem = loItems.CreateItem()  && Create a new element type item
loNewItem.Sku = "NEWITEM"
loNewItem.Descript = "New Item"

loItems.AddItem(loNewItem)
? loItems.Count   && 49

loItems.RemoveItem(48)  && remove last item (remember 0 based)
? loItems.Count   && 48

loItems.Clear()
? loItems.Count   && 0

*** No use ComArray instead of .NET array as a parameter
loBridge.InvokeMethod(loService,"UpdateInventoryItems",loArray)  && ComArray used as .NET array parameter

*** In most cases the following should also work by passing the actual array instance
loService.UpdateInventoryItems(loItems.Instance)

Create an array and pass it to .NET
Along the same lines you can create a new ComArray instance and start adding items to it:

loBridge = CREATEOBJECT("wwDotNetBridge")

loArray = loBridge.CreateArray("Westwind.WebStore.wws_ItemRow")

*** Create the item to add
loItem = loBridge.CreateInstance("Westwind.WebStore.wws_ItemRow")
loItem.SKU = "NEWITEM"
loItem.Descript = "blah blah"
loItem.Price = 25.00

*** Add the item to the array
loArray.AddItem(loItem)

*** Call the service with the array
loBridge.InvokeMethod(loService,"UpdateInventoryItems",loItems)  && ComArray used as .NET array

See also

Accessing Arrays

© West Wind Technologies, 2004-2017 • Updated: 08/03/15
Comment or report problem with topic