Return Array From WebService

Here’s the simplest way to accomplish what you want. Have your webservice return a simple string array:

[WebMethod]
public string[] getPersonIds(string Id)
{
//substitute code to actually populate the array with the dataset data
string[] personIds = {“ADS34579354”, “ASR34579354”, “TYU34579354”};
return personIds;
}

In the client:

//testWs = proxy object.
TestWs.TestWS testWs = new TestWs.TestWS();
string[] personIds = testWs.getPersonIds();
foreach (string personId in personIds)
{
Console.WriteLine(personId);
}
If you actually want to return an object called “PersonResult” with named  instances of “ID” then here’s what you do:

in your WebServices code:

create a class called “PersonResult”, with the one single “Id” member,  declared as a primitive array:
class PersonResult
{
public string[] Id;
}
and your [WebMethod] function should look like this:

[WebMethod]
public PersonResult getPersonIds()
{
PersonResult personResult = new PersonResult();
personResult.Ids.Add(“ADS34579354”);
personResult.Ids.Add(“ASR34579354”);
personResult.Ids.Add(“TYU34579354”);
return personResult;
}

in the client, here’s your code to obtain the instance of “PersonResult”  from the webservice:

TestWs.TestWS testWs = new TestWs.TestWS();
//define a variable of type TestWs.PersonResult, as defined by the proxy  class
TestWs.PersonResult personResult = testWs.getPersonIds();
foreach (string personId in personResult.Ids)
{
Console.WriteLine(personId);
}
Hope that helps!

Jorge

http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.framework.aspnet.webservices/2005-06/msg00068.html

Categories

No Responses

Leave a Reply

Your email address will not be published. Required fields are marked *