wwDotNetBridge::CreateArrayOnInstance

This method creates a new instance of a one dimensional array on an existing .NET object.

You can pass in either a numeric size in which case the array is created uninitialized (ie. each of the items are null or default .NET values). If you pass in any non-numeric value for the third parameter a 1 element array is created with the first element set to the item passed in.

Note that AddArrayItem() can perform the same task as this method as it too can create the array if it doesn't exist.

Due to limitations in how VFP handles .NET arrays arrays must always be defined on an object and can't be directly accessed as objects in VFP. For this reason the syntax requires both the base object reference and the array property as a string.

loBridge = CREATEOBJECT("wwDotNetBridge")

*** Create a .NET class that has PersonArray Property
loItem = loBridge.CreateInstance("Westwind.WebConnection.VfpTestClass")

*** Create an array with 2 elements 
? loBridge.CreateArrayOnInstance(loItem,"PersonArray",2)

loPerson = loBridge.GetArrayItem(loItem,"PersonArray",0)
loPerson.Name = "Rick"

loPerson2 = loBridge.GetArrayItem(loItem,"PersonArray",1)
loPerson2.Name = "Jim"

*** This works too
loBridge.SetpropertyEx(loRequest,"ResponseGroup[0].Name","Frankie")

*** Set two array items
loBridge.SetPropertyEx(loItem,"PersonArray[0]",loPerson)
loBridge.SetPropertyEx(loItem,"PersonArray[1]",loPerson2)

Alternately you can pass an object for the third parameter and create a one element array with a value assigned, then use AddArrayItem to add more items:

loBridge = CREATEOBJECT("wwDotNetBridge")
loItem = loBridge.CreateInstance("Westwind.WebConnection.VfpTestClass")

*** Create a new person
loPerson = loBridge.CreateInstance("Westwind.WebConnection.Person")
loPerson.Name = "Jim johnson"
loPerson.Company = "Temporary traders"

*** Create 1 element array and add the new person object
loBridge.CreateArrayOnInstance(loItem,"PersonArray",loPerson)
loPerson = loBridge.GetArrayItem(loItem,"PersonArray",0)
? loPerson.Name
? loPerson.Company

loPerson = loBridge.CreateInstance("Westwind.WebConnection.Person")
loPerson.Name = "Billy Bopp"
loPerson.Company = "Boppers Inc."

loBridge.AddArrayItem(loItem,"PersonArray",loPerson)

*** Length of the array - 2
? loBridge.GetPropertyEx(loItem,"PersonArray.Length")

*** Get the second item and display
loPerson = loBridge.GetArrayItem(loItem,"PersonArray",1)
? loPerson.Name
? loPerson.Company

The first mechanism will be more efficient as it pre-allocates the array in .NET. The latter approach is easier to write, but it causes the array to be copied in order to allow resizing of the array dynamically.

o.CreateArrayOnInstance(loBaseObject, lcArrayProperty, [lnSize | loData])

Return Value

.T. or .F.

Parameters

loBaseObject
The .NET object instance that contains the array member.

lcArrayProperty
The name of the property on loBaseObject that is the array.

lnSize | loData
This parameter is variable: If a numeric value is passed the array is sized to the specified value with all items uninitialized. If anything but a number is passed a 1 element array is created and the first data item is set to loData


See also:

Class wwDotNetBridge | Accessing Arrays | wwDotNetBridge::AddArrayItem

© West Wind Technologies, 2004-2020 • Updated: 02/07/14
Comment or report problem with topic