Returning Classes
A class is a compound data type completely defined by you, the programmer. Much like the enumerations shown in Listings 10.5 and 10.6, a class is defined in your code, and the variables can be declared as the type of your class.
Listing 10.7 shows a simple class declared in Visual Basic. This class includes two variables, a string type called Name and an Integer type called ID. Listing 10.8 shows the same class declared in C#. Applications using your service would be unable to tell the two of them apart.
NOTE
Although classes can contain any data type, even enumerations and other classes, a complete discussion of classes is beyond the scope of this book. The examples used in this hour are very basic.
Listing 10.7 A Class Declaration in VB
1: Public Class Person 2: Public Name As String 3: Public ID As Integer 4: End Class
Listing 10.8 A Class Declaration in C#
1: public class Person 2: { 3: public string Name; 4: public int ID; 5: }
When you wish to use your class, you declare a variable as the type of your classin this example, Person. You then use the New keyword to actually create the object. With the object created, you can now begin setting its properties. In Listing 10.9 (Visual Basic) and Listing 10.10 (C#), we set the Name property of our new Person object, oPerson, equal to "Jim Smith" and the ID property to 5.
Listing 10.9 Returning a Person Object in VB
1: <WebMethod()> Public Function ClassReturn() As Person 2: 3: Dim oPerson As New Person() 4: 5: oPerson.Name = "Jim Smith" 6: oPerson.ID = 5 7: 8: Return oPerson 9: End Function
Listing 10.10 Returning a Person Object in C#
1: [WebMethod] 2: public Person ClassReturn() 3: { 4: Person oPerson = new Person(); 5: 6: oPerson.Name = "Jim Smith"; 7: oPerson.ID = 5; 8: return oPerson; 9: }
To return your oPerson object to client applications, you declare your method to return type Person, as seen in line 1 of Listing 10.9, and then you simply return your oPerson object, as in Line 8 of the same listing.
When your object is returned, individual type information is not included. Instead, as shown in Figure 10.3, the objects are typed to their variable names. In this example, the return type is Person and contains types Name and ID.
Figure 10.3 Returning an object of type Person.
Returning an Array of Classes
Returning an array of classes is very similar to returning a single class. First, you declare your method to return an array of some object type. Listing 10.11, for example, shows a method returning type Person.
Next, you create your array of objects, in our case three of them, and set their properties. When you are done setting their properties and running any other code that your method may require, you return your array, as shown in line 15.
Listing 10.11 Returning an Array of Person Objects in VB
1: <WebMethod()> Public Function ClassesReturn() As Person() 2: 3: Dim oPerson(2) As Person 4: 5: oPerson(0) = New Person() 6: oPerson(0).Name = "Jim Smith" 7: oPerson(0).ID = 1 8: oPerson(1) = New Person() 9: oPerson(1).Name = "Edgar Smith" 10: oPerson(1).ID = 2 11: oPerson(2) = New Person() 12: oPerson(2).Name = "Mary Smith" 13: oPerson(2).ID = 3 14: 15: Return oPerson 16: End Function
Listing 10.12 shows how C# returns an array of Person objects in the same manner as the previous Visual Basic example.
Listing 10.12 Returning an Array of Person Objects in C#
1: [WebMethod] 2: public Person[] ArrayClassReturn() 3: { 4: Person[] oPerson = new Person[3]; 5: 6: oPerson[0] = new Person(); 7: oPerson[0].Name = "Jim Smith"; 8: oPerson[0].ID = 1; 9: oPerson[1] = new Person(); 10: oPerson[1].Name = "Edgar Smith"; 11: oPerson[1].ID = 2; 12: oPerson[2] = new Person(); 13: oPerson[2].Name = "Mary Smith"; 14: oPerson[2].ID = 3; 15: 16: return oPerson; 17: }
The returned SOAP document for the array of Person objects is very similar to that shown in the previous example, only now, as shown in Figure 10.4, all three of your Person objects are wrapped in an XML tag showing a type ArrayofPerson.
Figure 10.4 Returning an array of objects of type Person.