Using ComArray Instances

ComArray's are typically used in three distinct scenarios:

  • Receiving an Array from a .NET method call result or property value
  • Updating a passed array, updating it and passing it back
  • Creating a new array and passing it to .NET

Retrieving ComArray Instances from .NET Methods and Properties

The most common use case is receiving ComArrays from method call results or property value access. The following examples receives a ComArray as a result of a .NET method call that returns an array of item objects:

*** Method returns wws_Item[] array from .NET - FoxPro gets ComArray instance
loItems = loBridge.InvokeMethod(loInstance,"GetInventoryItemsArray","Books")

*** .NET Arrays are 0 based!
FOR lnX = 0 to loItems.Count -1
    loItem = loItems.Item(lnX)  && Retrieve an array item
    ? loItem.Sku + " " + loItem.Descript + " " + TRANS(loItem.Price)
ENDFOR

Manipulate an existing Array

Once you have an array you can manipulate items in the array by simply retrieving and then changing the item:

loItem = loItems.Item(0)  && Retrieve an array item
loItem.Sku = "NewSku"
loItem.Descript = "New Description"

You can then pass it back to .NET by using either InvokeMethod() or SetProperty():

*** Pass loItems which is the ComArray instance in place of the .NET wws_Items[] array loBridge.InvokeMethod(loInstance,"SaveItems",loItems)

The actual .NET item is updated in this case since the array exists natively in .NET.

Creating an Array and Adding Items

*** Create an array of wws_Item[]
loItems = loBridge.CreateArray("Westwind.wws_Item")

loItem = loItems.CreateItem()
loItem.Sku = "NewItem"
loItem.Descript = "New Item"

*** Add the new item to the array
loItems.Add(loItem)

Passing Arrays back to .NET Methods and Properties

You can pass arrays to .NET using COM array when using InvokeMethod() and SetProperty() which provides an easy way to create or manipulate .NET arrays inside of FoxPro code and pass it back to .NET.

*** Create an array of wws_Items
loItems = loBridge.CreateArray("Westwind.wws_Item")

loItem = loItems.CreateItem()
loItem.Sku = "NewItem"
loItem.Descript = "New Item"

*** Add the new item to the array
loItems.Add(loItem)

*** Pass the Com Array in place of the wws_Item[] array parameter
loBridge.InvokeMethod(loInstance,"SendInventoryItemsArray",loItems)

*** Or alternately set a property
loBridge.SetProperty(loInstance,Items,loItems)

© West Wind Technologies, 2004-2020 • Updated: 12/22/16
Comment or report problem with topic