Click or drag to resize

Handling RAPID arrays

Overview

In RAPID you can have up to three dimensional arrays. These are accessible by using a RapidData object like any other RAPID data.

There are mainly two ways of accessing each individual element of an array: by indexers or by an enumerator.

Tip Tip
A convenient and user-friendly way of reading and writing array elements is using the standard Program Data view of the FlexPendant. You provide the element you want to have displayed as argument, and the user can view or manipulate the item the way it is usually done on the FlexPendant. For more information, see Using standard dialog box to modify data.
ArrayData object

If the RapidData references a RAPID array is Value property returns an object of ArrayData type. Before making a cast, check the type using the is operator or by using the IsArray property on the RapidData object.

C#
RapidData rd = aController.Rapid.GetRapidData("T_ROB1", "User", "string_array");
if (rd.IsArray)
{
ArrayData ad = (ArrayData)rd.Value;
.....
}
VB
Dim RD As RapidData = AController.Rapid.GetRapidData("T_ROB1", "User", "string_array")
If RD.IsArray Then
Dim AD As ArrayData = DirectCast( RD.Value,ArrayData)
.....
End If
Array dimensions

The dimension of the array is returned by the Rank property. If you need to check the length of the individual arrays you can use the GetLength method on the ArrayData object passing the dimension index as argument.

C#
int aRank = ad.Rank;
int len = ad.GetLength(aRank);
VB
 Dim ARank As Integer = AD.Rank
Dim Len As Integer = AD.GetLength(ARank)
Array item access by using indexers

By the use of indexers you can access each array element, even in three dimensional arrays. A combination of the GetLength method and For loops makes it possible to access any item:

C#
            double aSum = 0d;
            Num aNum;
            if (ad.Rank == 1) {
            for (int i = 1; i <= ad.Length; i++)
            {
            aNum = (Num)ad[i];
            aSum += (double)aNum;
            }
            }
            else if (ad.Rank == 2)
            {
            for(int i = 1; i< ad.GetLength(1); i++) 
{
for (int j = 1; j <= ad.Length; j++)
            {
            aNum = (Num)ad[i,j];
            aSum += (double)aNum;
            }
            }
            }
            else {
            for(int i = 1; i< ad.GetLength(1); i++) 
{
for(int j = 1; j< ad.GetLength(2); j++) 
{
for (int k = 1; k <= ad.GetLength(3); k++)
            {
            aNum = (Num)ad[i, j, k];
            aSum += (double)aNum;
            }
            }
            }
            }
VB
Dim aSum As Double = 0R
Dim aNum As Num
If ad.Rank = 1 Then
For I As Integer = 1 To ad.Length
aNum = DirectCast(ad(i), Num)
aSum += CDbl(aNum)
Next
ElseIf ad.Rank = 2 Then
For i As Integer = 1 To ad.GetLength(1) - 1
For j As Integer = 1 To ad.Length
aNum = DirectCast(ad[i, j], Num)
aSum += CDbl(aNum)
Next
Next
Else
For i As Integer = 1 To ad.GetLength(1) - 1
For j As Integer = 1 To ad.GetLength(2) - 1
For k As Integer = 1 To ad.GetLength(3)
aNum = DirectCast(ad(i, j, k), Num)
aSum += CDbl(aNum)
Next
Next
Next
End If
Array item access using enumerator

You can also use the enumerator operation (foreach) like it is used by collections in .NET. Note that it can be used for both one dimension and multi-dimensional arrays to access each individual element. The previous example is a lot simpler this way:

C#
double aSum = 0d;
Num aNum;
foreach(Num aNum in ad)
{
aSum += (double)aNum;
}
VB
 Dim aSum As Double = 0R
 Dim aNum As Num
 For Each aNum As Num In AD
aSum += CDbl(aNum)
 Next