Returning Arrays of Structures

Many times, applications need to return arrays of structured data. Frequently, this data is stored in a database or on a device and needs to be transmitted en masse. For example, when requesting the details of a purchase order, it makes sense to return the lines and delineate the parts of the line. A line item might contain the following details:

  • Part number

  • Part name

  • Part description

  • Quantity ordered

  • Price per part

As we saw with the GetPerson example, a bit of structured data can contain other structures. Likewise, arrays can appear anywhere within the data. Arrays are very powerful and are often quite handy. Fortunately, .NET handles these well too. To keep things simple, we will not introduce any new classes for this example (see Listing 1.5). Instead, we will take an example that returns an array of names. This example will return a list of all the authors (contributing authors included) involved with this book.

Listing 1.5. Source for the FirstService.GetAuthorNames Function
<WebMethod()> Public Function GetAuthorNames() As Name()
    Dim firstNames() As String = {"Scott", "Michael", "Jeffrey", "Deon"}
    Dim lastNames() As String = {"Seely", "Carnell", "Huntsman", "Schaffer"}

    ' Array will be dimensioned 0 to firstNames.Length - 1. The
    ' assumption here is that firstNames.Length == lastNames.Length
    Dim retval(firstNames.Length - 1) As Name
    Dim index As Long
    ' Instantiate the four names
    For index = 0 To retval.Length - 1
        retval(index) = New Name()
        retval(index).First = firstNames(index)
        retval(index).Middle = ""
        retval(index).Last = lastNames(index)
    Next

    GetAuthorNames = retval
End Function
					

To make things very simple, all the first names are stored in one array and the last names in another. Then, as the code loops through initializing the individual array elements, it also sets the values for the Name.First, Name.Middle, and Name.Last. Again, we will test this Web Method using the HTTP/GET interface. The message exchange will look like the following:

Request:

GET /Chapter1/FirstService.asmx/GetAuthorNames? HTTP/1.1
Host: localhost

Response:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfName xmlns="http://tempuri.org/">
  <Name>
    <First>string</First>
    <Middle>string</Middle>
    <Last>string</Last>
  </Name>
  <Name>
    <First>string</First>
    <Middle>string</Middle>
    <Last>string</Last>
  </Name>
</ArrayOfName>
					

When choosing the Invoke button on the generated Web page, the names are returned with the following XML:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfName
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns="http://tempuri.org/">
  <Name>
    <First>Scott</First>
    <Middle />
    <Last>Seely</Last>
  </Name>
  <Name>
    <First>Michael</First>
    <Middle />
    <Last>Carnell</Last>
  </Name>
  <Name>
    <First>Jeffrey</First>
    <Middle />
    <Last>Huntsman</Last>
  </Name>
  <Name>
    <First>Deon</First>
    <Middle />
    <Last>Schaffer</Last>
  </Name>
</ArrayOfName>

Once again, the serialization is done without any effort on the developer's part.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.26.138