Results 1 to 4 of 4

Thread: Return two-dimensional string from .net webservice

  1. #1

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Return two-dimensional string from .net webservice

    I'm having difficulties with this...

    I need to return a two-dimensional string array from a web service....

    It seems simple enough...

    VB Code:
    1. <WebMethod> Public Function ReturnMultiDimArray() As String()()
    2.   Dim y As String()
    3.   Dim z As String()
    4.   Dim x As String()() = New String()() {y, z}
    5.   Return x
    6. End Function

    However, I need to populate the two dimensions (y and z) dynamically from a datareader.

    So I tried using ( ) arrayLists for y and z, and using to arraylist.ToArray method, but of course, I get an invalid cast exception when trying to build the 2-d string array:
    VB Code:
    1. Dim x As String()() = New String()() {y.ToArray, z.ToArray}

    I saw StringDictionary, but that doesn't serialize... and I don't want to waste hours figuring out to implement Iserializable and implenets getObject....

    Any ideas?

  2. #2

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Well, I didn't solve the problem as I intended, however, I was able to return the data via XML, so I guess that is a feasible solution. I still would like to know if anyone had an easier way:
    VB Code:
    1. Imports System.Xml.Serialization
    2. ...
    3. Sqlconnection1.Open()
    4.  
    5.         dr = Sqlcommand1.ExecuteReader(CommandBehavior.CloseConnection)
    6.         While dr.Read
    7.             x.Add(New SubmissionItem(dr(0), dr(1)))
    8.         End While
    9.         dr.Close()
    10.  
    11.         Dim sw As System.io.StringWriter = New System.IO.StringWriter
    12.         Dim s As XmlSerializer = New XmlSerializer(GetType(SubmissionTypeArrayList))
    13.         s.Serialize(sw, x)
    14.         Return x

    Classes created to use code above:


    VB Code:
    1. Imports System.Xml.Serialization
    2.  
    3.     <XmlRoot("SubmissionTypes")> Public Class SubmissionTypeArrayList
    4.         Private _SubmissionList = New ArrayList
    5.         Public CarsArrayList()
    6.         <XmlArray("SubmissionList"), _
    7.         XmlArrayItem("SubmissionTypeItem", GetType(SubmissionItem))> _
    8.         Public Property SubmissionCollection() As ArrayList
    9.             Get
    10.                 Return _SubmissionList
    11.             End Get
    12.             Set(ByVal Value As ArrayList)
    13.                 _SubmissionList = Value
    14.             End Set
    15.         End Property
    16.  
    17.         Public Property Submission(ByVal index As Integer)
    18.             Get
    19.                 Return _SubmissionList(index)
    20.             End Get
    21.             Set(ByVal Value)
    22.                 If index > _SubmissionList.count - 1 Then
    23.                     _SubmissionList.add(Value)
    24.                 Else
    25.                     _SubmissionList(index) = Value
    26.                 End If
    27.             End Set
    28.         End Property
    29.  
    30.         Public Sub Add(ByVal s As SubmissionItem)
    31.             _SubmissionList.add(s)
    32.         End Sub
    33.  
    34.     End Class
    35.  
    36.     Public Class SubmissionItem
    37.         <XmlElement("fksubmission")> Public fkSubmission As String
    38.         <XmlElement("submissiontype")> Public SubmissionType As String
    39.         Public Sub New()
    40.  
    41.         End Sub
    42.  
    43.         Public Sub New(ByVal s1 As String, ByVal s2 As String)
    44.             fkSubmission = s1
    45.             SubmissionType = s2
    46.         End Sub
    47.     End Class

  3. #3
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Try this...
    VB Code:
    1. Dim x As String()() = New String()() {y.ToArray(GetType(String)), z.ToArray(GetType(String))}

    From your first attempt, that is.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  4. #4

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Thanks crpt, that solves the initial request.

    Of course, now I have to decide which would be easier to populate a dropdownbox in old ASP from:
    returning a 2-d jagged string array
    VB Code:
    1. - <ArrayOfString>
    2.   <string>1</string>
    3.   <string>2</string>
    4.   </ArrayOfString>
    5. - <ArrayOfString>
    6.   <string>Injury</string>
    7.   <string>Suspicious Activity</string>
    8.   </ArrayOfString>
    9.   </ArrayOfArrayOfString>

    Or the custom class implementation:
    VB Code:
    1. <SubmissionTypeItem>
    2.   <fksubmission>1</fksubmission>
    3.   <submissiontype>Injury</submissiontype>
    4.   </SubmissionTypeItem>
    5. - <SubmissionTypeItem>
    6.   <fksubmission>2</fksubmission>
    7.   <submissiontype>Suspicious Activity</submissiontype>
    8.   </SubmissionTypeItem>
    9.   </SubmissionList>

    ???

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width