Returning an ArrayList from a WebMethod()

Discussion in 'ASP .Net Web Services' started by circuit_breaker@canada.com, Jan 3, 2006.

  1. Guest

    Hi,

    I'm trying to call a WebMethod that returns an ArrayList from an
    ASP.Net Web Page. The signature of the WebMethod is as follows:

    --------
    <WebMethod()> _
    Public Function RetrieveCategories() As ArrayList
    -------
    The WebMethod() is just retrieving 5 records from a table in a
    database.

    On the client side, I have the following:

    --------
    Dim xws As New Hello.Service1
    Dim aList As New ArrayList

    aList = xws.RetrieveCategories()
    ---------
    but the xws.RetrieveCategories() part is being underlined, giving me
    this message:

    "value of type '1-dimensional array of System.Object' cannot be
    converted to 'System.Collections.ArrayList'"


    As I understand, I might have to modify the stub but I'm not quite sure
    what to put in it...

    Thanks for your help.
    Regards, Steve.
     
    , Jan 3, 2006
    #1
    1. Advertising

  2. Cast it to ArrayList, in C#
    aList = (ArrayList)xws.RetriveCategories();

    Hope this helps
    Al


    "" wrote:

    > Hi,
    >
    > I'm trying to call a WebMethod that returns an ArrayList from an
    > ASP.Net Web Page. The signature of the WebMethod is as follows:
    >
    > --------
    > <WebMethod()> _
    > Public Function RetrieveCategories() As ArrayList
    > -------
    > The WebMethod() is just retrieving 5 records from a table in a
    > database.
    >
    > On the client side, I have the following:
    >
    > --------
    > Dim xws As New Hello.Service1
    > Dim aList As New ArrayList
    >
    > aList = xws.RetrieveCategories()
    > ---------
    > but the xws.RetrieveCategories() part is being underlined, giving me
    > this message:
    >
    > "value of type '1-dimensional array of System.Object' cannot be
    > converted to 'System.Collections.ArrayList'"
    >
    >
    > As I understand, I might have to modify the stub but I'm not quite sure
    > what to put in it...
    >
    > Thanks for your help.
    > Regards, Steve.
    >
    >
     
    Albert Pascual, Jan 3, 2006
    #2
    1. Advertising

  3. Steve,

    If you take a look the wsdl for the RetrieveCategories method, you will
    notice that it does not mention the ArrayList object. Instead, it just
    interprets as an array of any type. Hence, the array of objects. This
    explains why you get a cast exception when you're trying to convert the
    System.Object() the web service returns to an ArrayList. Since you're
    going through SOAP, there's no more ArrayList.

    If you want to use an ArrayList in your object, you need to do the
    following:

    Dim xws As New Hello.Service1
    Dim aList As New ArrayList

    Dim data() as Object = xws.RetrieveCategories()
    aList.AddRange(data)

    Hope this helps/clarifies some of this!

    - Javier
     
    Javier G. Lozano, Jan 4, 2006
    #3
  4. Guest

    Exactly what I was looking for. And if I understand well, the data()
    as Object is there for early-binding. It will work without but the
    late-binding situation would make it slower.

    Thanks!
     
    , Jan 15, 2006
    #4
  5. "And if I understand well, the data() as Object is there for
    early-binding. It will work without but the late-binding situation
    would make it slower."

    I'm not sure if I understand your statement correctly. The reason why
    it's an array of object is because the WSDL contract specifies that the
    result type for your method is an collection. An ArrayList is specific
    to .NET (and Java), so in order to make things more interoperable, an
    array of Object is returned.
     
    Javier G. Lozano, Jan 17, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex

    Returning ArrayList from WebMethod, best practices

    Alex, Jul 16, 2003, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    122
  2. Tobias Lekman

    Returning Enumeration or Exception from WebMethod?

    Tobias Lekman, Aug 9, 2003, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    166
    Marina
    Aug 11, 2003
  3. patrick

    WebMethod returning XMLdataDocument

    patrick, Feb 11, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    260
    patrick
    Feb 11, 2004
  4. WebMethod returning a object in a COM dll

    , Feb 16, 2005, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    118
    Keenan Newton
    Feb 17, 2005
  5. Steven

    error using arraylist as webmethod argument

    Steven, Feb 12, 2006, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    184
    Steven
    Feb 12, 2006
Loading...

Share This Page