Web Service returning ArrayList

janilane

Active member
Joined
Jan 23, 2008
Messages
30
Programming Experience
Beginner
I'm trying to do test run of how i'm gonna output my data from the database so I've created dummy info. I have a web method that returns an ArrayList of students. Each student object has several addresses tied to it, which I have defined as another arraylist. I was able to display the output but it is not in the sequence of order that I wanted. Instead of this which I wanted:

Student1
S1Address1
S1Address2
S1Address3
Student2
S2Address1
S2Address2
Student3
S3Address1
S3Address2
S3Address3

I got this:

S1Address1
S1Address2
S1Address3
S2Address1
S2Address2
S3Address1
S3Address2
S3Address3
Student1
Student2
Student3


I pressume this is because the inner ArrayList was read first and then the outter Arraylist?


Below is a sample code:

VB.NET:
<WebMethod(), XmlInclude(GetType(Student)), XmlInclude(GetType(Address))> Public Function StudCollection() As ArrayList
        '	HelloWorld = "Hello World"


        Dim alStudentCollection As ArrayList = New ArrayList()
        Dim oStudent As New Student()

        oStudent.CtContactID = 44111
        oStudent.StudentName = "Lynch Mob"

        Dim oAddress As New Address()

        oAddress.AddressID = 1001
        oAddress.StreetName = "Water St"
        oAddress.SuburbName = "New Land"
        oAddress.City = "Waitakere"
        oAddress.Postcode = "0600"
        'add to address collection
        oStudent.AddAddress(oAddress)

        oAddress = New Address()

        oAddress.AddressID = 1002
        oAddress.StreetName = "Nikau Street"
        oAddress.SuburbName = "New Land"
        oAddress.City = "Chicago"
        oAddress.Postcode = "0600"
        oStudent.AddAddress(oAddress)

        oAddress = New Address()

        oAddress.AddressID = 1003
        oAddress.StreetName = "Pine Street"
        oAddress.SuburbName = "New Land"
        oAddress.City = "Waitakere"
        oAddress.Postcode = "0600"
        oStudent.AddAddress(oAddress)

        alStudentCollection.Add(oStudent)

        oStudent = New Student()

        oStudent.CtContactID = 44112
        oStudent.StudentName = "Michael Jordan"
        alStudentCollection.Add(oStudent)

        'check arraylist contents
        'Dim oStudObject As Student
        'Dim oAddrObject As Address

        'For Each oStudObject In alStudentCollection
        'MessageBox.Show(oStudObject.CtContactID & " = " & oStudObject.StudentName)
        'For Each oAddrObject In oStudObject.m_alAddress
        'MessageBox.Show(oAddrObject.AddressID & "=" & oAddrObject.StreetName)
        'Next
        'Next
        Return alStudentCollection
    End Function

Public Class Student
    Private m_iCtContactID As Integer
    Private m_sStudName As String
    Public m_alAddress As New ArrayList()

    Public Property CtContactID() As Integer
        Get
            Return m_iCtContactID
        End Get
        Set(ByVal value As Integer)
            m_iCtContactID = value
        End Set
    End Property

    Public Property StudentName() As String
        Get
            Return m_sStudName
        End Get
        Set(ByVal value As String)
            m_sStudName = value
        End Set
    End Property

    Public Sub AddAddress(ByVal oAddress As Address)
        m_alAddress.Add(oAddress)
    End Sub
End Class



Public Class Address
    Private m_iAddressID As Integer
    Private m_sStreetName As String
    Private m_sSuburbName As String
    Private m_sCity As String
    Private m_sPostcode As String

    Public Property AddressID() As Integer
        Get
            Return m_iAddressID
        End Get
        Set(ByVal value As Integer)
            m_iAddressID = value
        End Set
    End Property

    Public Property StreetName() As String
        Get
            Return m_sStreetName
        End Get
        Set(ByVal value As String)
            m_sStreetName = value
        End Set
    End Property

    Public Property SuburbName() As String
        Get
            Return m_sSuburbName
        End Get
        Set(ByVal value As String)
            m_sSuburbName = value
        End Set
    End Property

    Public Property City() As String
        Get
            Return m_sCity
        End Get
        Set(ByVal value As String)
            m_sCity = value
        End Set
    End Property

    Public Property Postcode() As String
        Get
            Return m_sPostcode
        End Get
        Set(ByVal value As String)
            m_sPostcode = value
        End Set
    End Property

End Class
 
Back
Top