Home > Articles > Programming

  • Print
  • + Share This
This chapter is from the book

Passing Arrays of Primitive Data Types

If you have never written code that returned an array of some data type before, there are a few things to keep in mind. Look at the code in Listing 10.3. In line 1, you will notice that the data type is listed as Integer(). The () indicate that our method will be returning an array of some unknown size. For those of you following along in C#, line 2 of Listing 10.4 shows the equivalent declaration using int[] to return an array of integers.


As shown in Table 10.1, int is the C# version of a Visual Basic Integer.

Listing 10.3 Returning an Array in VB

1:  <WebMethod()> Public Function ArrayReturn() As Integer()
2:    Dim a(5) As Integer
3:    Dim i As Integer
5:    For i = 0 To 5
6:      a(i) = i + 1
7:    Next
9:    Return a
10:  End Function

Listing 10.4 Returning an Array in C#

1:  [WebMethod]
2:  public int[] ArrayReturn()
3:  {
4:   int[] a = new int[6];
5:   for (int i=0; i <6; i++ )
6:   {
7:    a[i] = i + 1;
8:   }
9:   return a;
10:  }

When returning an array, simply return the array name without any brackets, as shown in line 9 of both Listing 10.3 and 10.4. This will return the entire array. Client code can check for the upper bounds of the array in order to retrieve the number of elements that the array contains.

When an array is returned from an XML Web service, the SOAP document contains the type declaration and value for every value in the array (see Figure 10.1) that is contained inside an XML tag proclaiming the contents to be type ArrayofInt.

Figure 10.1 Returning an array from your XML Web service.

  • + Share This
  • 🔖 Save To Your Account