Array Manipulation Methods

Arrays don't translate well into FoxPro with native Com Interop

.NET Array manipulation from FoxPro is tricky because .NET arrays and collections don't transfer as .NET objects into FoxPro when marshalled over COM. .NET Arrays are converted into FoxPro arrays, and once converted they lose their .NET connection - ie. you can't easily update the array once it's been marshalled to FoxPro.

The ComArray class addresses this by leaving arrays in .NET and using a proxy interface to manipulate the arrays to add, access, update and remove items from arrays using a simple object interface. The lower level functions described in this topic do the same but using more intricate syntax and logic to access and assign arrays indirectly. Generally using ComArray is easier, producing easier to read and maintain code and are more efficient since data stays complete in .NET except for indivdual items retrieved.

Note although these direct Array Manipulation methods exist on the wwDotnetBridge class and .NET object, we highly recommend you don't use them unless you absolutely must. Instead use the more flexible ComArray class that abstracts .NET arrays and allows much easier manipulation of .NET arrays using a simple object model.

Using ComArray for easy Array/Collection Access

Using ComArray

ComArray allows array manipulation more directly, even when dealing with arrays that are defined on existing instances of objects:

*** Get an Array from a .NET loObject.Persons property
loPersons = loBridge.GetProperty(loObject,"Persons") 

*** Create a new item based on Array item type
loNewPerson = loPersons.CreateItem() 
loNewPerson.Name = "John Doe"
loNewPerson.City = "Nowheres"

*** Add Item to the actual array
loPersons.AddItem(loPerson)

*** Or you can create an array from scratch
loPersons = loBridge.CreateArray("MyApp.Person")

*** Add item
loNewPerson = loPersons.CreateItem()
loNewPerson.Name = "John Doe"
loNewPerson.City = "Nowheres"
loPersons.AddItem(loNewPerson)

*** Assign array to a Person[] Array property
loBridge.SetProperty(loObject,"Persons",loPersons)

*** Pass the array as a  Person[] parameter
loBridge.InvokeMethod(loObject,"PassArray",loPersons)

In most cases ComArray is preferrable over using the following array functions.

Using the Array Manipulation Methods on wwDotnetBridge

Alternately for quick array access operations that require access or assignment of array members you can also use the following array methods without the intermediary ComArray object.

The array manipulation methods all below operate on an array parent property, rather than directly operating on an Array instance to avoid having to transfer the array object into Visual FoxPro. As such all methods take a parent object reference and an arrayname as the first couple of parameters.

In addition to these explicit array functions, GetPropertyEx and SetPropertyEx may be used to retrieve properties from arrays using array [] bracket syntax:

*** Get an Array item
loPerson = loBridge.GetPropertyEx(loObject,"PersonArray[0]")

*** Retrieve a property value
lcName = loBridge.GetPropertyEx(loObject,"PersonArray[0].Name")

*** Assign an existing array item
loBridge.SetPropertyEx(loObject,"PersonArray[0]",loPerson)

Use ComArray Where Possible

The overall suggestion is to use ComArray whenever possible as it provides the most reliable and consistent interface to manipulating the array in memory in .NET without having to transfer it into FoxPro.

Use the other support functions when you need have no other way to initially assign or update an array only and then use GetProperty() to retrieve the ComArray instance to work with.

See also

Class ComArray | Using ComArray Instances for Array Access

© West Wind Technologies, 2004-2020 • Updated: 04/27/17
Comment or report problem with topic