Returning an ArrayList from a WebMethod()

  • Thread starter circuit_breaker
  • Start date
C

circuit_breaker

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.
 
A

Albert Pascual

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

Hope this helps
Al
 
J

Javier G. Lozano

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
 
C

circuit_breaker

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!
 
J

Javier G. Lozano

"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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top