Share via


Pass Strongly Typed Arrays Sample

File: ...\Samples\Solution\Toledo\ComArray.scx

This sample demonstrates how you can pass a strongly typed array by reference to a COM server using the COMARRAY function. The COM server performs calculations and modifies the contents of the array. The client form then uses the DIMENSION command with the AS clause to declare the strongly typed array.

In this sample, the COM server is written in Visual FoxPro. The sample also demonstrates the interface declaration syntax for specifying ByRef type arrays in the generated COM type library.

When opening the sample, you are prompted to register a COM server. The COM server is removed from the registry when you close the sample.

To pass a matrix as a strongly typed array to a COM server

  • In the sample, type values into the matrix and click Determinant.

The COM server calculates the determinant of the matrix using Gauss-Jordan Reduction.

For more information, see COMARRAY( ) Function and DIMENSION Command.

Passing Strongly Typed Arrays

In the sample, the form uses DIMENSION...AS to declare a strongly typed array as show in the custom CallCOMServer method:

DIMENSION laMatrix(ThisForm.Matrixdim, ThisForm.Matrixdim) AS Double

The array is passed by reference to the Visual FoxPro COM server using the COMARRAY( ) function:

* Instantiate COM server.
loMatrixCalc = CREATEOBJECT("VFPCOMArray.COMARRAYDemo")
* Array is one based and is passed by reference.
COMARRAY(loMatrixCalc,11) 
* Pass array to COM server by reference "As Double".
lnResult = loMatrixCalc.Determinant(@laMatrix)

Passing COM Parameters By Reference

In the sample, the custom RegisterCOMServer method contains code that compiles a Visual FoxPro COM server on the fly and registers it. The COM server code illustrates the interface declaration syntax for specifying ByRef typed arrays in the generated COM type library as follows:

FUNCTION Determinant(mat[] AS Double @) AS Double

The array is correctly written to the type library as a strongly typed array. The at sign (@) indicates that the array is passed ByRef in the type library.

See Also

Other Resources

Programming Solution Samples